2013-04-10 78 views
0

我需要將單選按鈕及其旁邊的文字包裝在標籤中,以方便用戶使用。在元素旁邊選擇文字(幾乎)

前幾天我有一個類似的問題,我有一個複選框,並在我有一個span元素後立即。我可以包裝這兩個元素。

我有這樣的HTML:

<span class="Attribute"> 
    <input type="radio" name="vMONHRDREM" value="S"> 
    <script type="text/javascript">gx.dom.setAttribute("vMONHRDREM","gxoch0","if(!(gx.evt.jsEvent(this))) return false;");</script> 
    Ativado 

    <input type="radio" name="vMONHRDREM" value="N"> 
    <script type="text/javascript">gx.dom.setAttribute("vMONHRDREM","gxoch0","if(!(gx.evt.jsEvent(this))) return false;");</script> 
    Desativado 
</span> 

不幸的是,我不能,因爲我用的是生成網頁的工具更改HTML結構。

我試圖做這樣的事情:

$("input[type=radio]").each(function(){ 
    alert($(this).next().next().text()); 
}); 

但它不工作。它不會返回任何東西。如果我更改爲$(this).next().text(),我會看到腳本文本。

更新: 最終解決方案。

$('.Attribute input[type="radio"]').each(function() { 
    $($(this).next('script')[0].nextSibling).wrap("<span />"); 
    $(this).addBack().add($(this).next('script')[0].nextSibling).wrapAll('<label></label>') 
}); 

謝謝你們

回答

2

輸入元素沒有文本內容,你有什麼是流氓textnodes,你會需要與輸入一起針對那些,然後將它們包裝標籤標籤:

$('.Attribute input[type="radio"]').each(function() { 
    $(this).addBack() 
      .add($(this).next('script')[0].nextSibling) 
      .wrapAll('<label></label>'); 
}); 

FIDDLE

+0

謝謝你的男人。正是我的意思!謝謝兩位! – lcssanches 2013-04-10 13:26:21

5
$("input[type=radio]").each(function(){ 
    var text = $(this).next().get(0).nextSibling.nodeValue; 
}); 
+0

太棒了!這給我看價值。但如何包裝這兩個元素?如果我使用... nextSibling.wrap(「