2016-08-02 48 views
0

下面是HTML和脚本代码nextuntil是不工作的分组输入与周围使用JQuery

$('input[type=radio]').each(function(){ 
$(this).add(this.nextSibling).nextUntil('input').addBack().wrapAll('<label/>'); 
}); 
<div id="formelements"> 
<input type="radio"/> yes <br/> 
<input type="radio"/> No <br/> 
<input type="radio"/>Select <b>or</b> deselect<br/> 
<input type="button" value="submit"/> 
</div> 

当我使上面的代码文本,标签标签被适当地包装为Yes和No元素但它没有正确标记为“选择或取消选择”。下面是上述代码
现有输出生成的输出:

<div id="formelements"> 
<label><input type="radio"> yes <br></label> 
<label><input type="radio"> No <br></label> 
<label><input type="radio">Select<b>or</b><br></label>deselect 
<input type="button" value="submit"> 
</div> 

需要的输出:

<div id="formelements"> 
<label><input type="radio"> yes <br></label> 
<label><input type="radio"> No <br></label> 
<label><input type="radio">Select<b>or</b>deselect<br></label> 
<input type="button" value="submit"> 
</div> 

回答

0

在这里你去,略有不同,因为文字是不实际HTML元素 演示:https://jsfiddle.net/veqbch51/

$('input[type=radio]').each(function() { 
    var $set = $(); 
    var nxt = this.nextSibling; 
    $set.push(this); 
    while (nxt) { 
    if (!$(nxt).is('input')) { 
     $set.push(nxt); 
     nxt = nxt.nextSibling; 
    } else break; 
    } 
    $set.wrapAll('<label/>'); 
}); 
+0

谢谢,它正在工作 – Manoj