开发者

How to make these radio buttons in the same <li> exclusive in jQuery?

<li>
<label>开发者_Python百科;<input type="radio" name="radio1" value="1">radio1</label>
<label><input type="radio" name="radio2" value="2">radio2</label>
...
</li>
<li>
....
</li>

I tried this but failed:

$('input[type="radio"]').click(function() {
    var $self = $(this);
    $.map($self.parents('li:first').find('input[type="radio"]'),function(n,i){
        if($(n) != $self)$(n).removeAttr('checked');
    });
});


Use the not function to remove the current item from the selection:

$('input:radio').click(function() {
    $(this).closest('li').find("input:radio").not(this).removeAttr('checked');
});

But if you just name then the same, they become exclusive automatically.

<label><input type="radio" name="radio1" value="1">radio1</label>
<label><input type="radio" name="radio1" value="2">radio2</label>


You shouldn't need jQuery to make radio buttons exclusive, just give them the same name.

<label><input type="radio" name="radio1" value="1">radio1</label>
<label><input type="radio" name="radio1" value="2">radio2</label>

and you should be good.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜