在同一个div中包装标签和输入

mad*_*mar 4 html jquery jquery-selectors dom-traversal

我试图将我的radio输入和它包装label在一个单独div但不知何故它不包装输入.我的代码是:

HTML是:

<div class="payment_processor-section">
    <input id="input_0" class="form-radio" type="radio" checked="checked"  name="payment_processor" value="1">
    <label for="input_0">Credit Card</label>
    <input id="input_1" class="form-radio" type="radio" name="payment_processor" value="0">
    <label for="input_1" style="">I will send payment by check</label>
</div>
Run Code Online (Sandbox Code Playgroud)

我正在尝试的Jquery是:

$('.payment_processor-section input').map(function(index){
    $(this, $("label[for='"+this.id+"']") ).wrapAll('<div class="abc">');
});
Run Code Online (Sandbox Code Playgroud)

请帮忙.

Ale*_*har 14

试试这个:

$('.payment_processor-section input').map(function(index){
  $(this).next().andSelf().wrapAll('<div class="abc">');
});
Run Code Online (Sandbox Code Playgroud)

小提琴


kid*_*won 8

或者http://jsfiddle.net/43jdq/

$('.payment_processor-section input').map(function(index){
 $(this).add($("label[for='"+this.id+"']")).wrapAll('<div class="abc">');
});
Run Code Online (Sandbox Code Playgroud)