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.
精彩评论