nextuntil不适用于使用JQuery将输入与周围文本分组



下面是HTML和脚本代码

$('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>

开始吧,略有不同,因为文本不是实际的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/>');
});

最新更新