2011-12-15 70 views
1

我有代码工作,但是当我添加另一个ul时,它会选中所有的复选框。jQuery select/unselect all,但只在同一个元素内

$('#checkall').click(
function(){ 
    $(this).closest('ul').find(':checkbox').attr('checked', this.checked); 
    $(this).closest('ul').find('span').toggleClass('checked', this.checked) 
}) 

<div class="checks"> 
<ul> 
    <li><label for="checkall"><div class="checker"><span class=""><input type="checkbox" id="checkall" name="about"></span></div><strong>About our program</strong></label></li> 
    <li><label for="about1"><div class="checker"><span><input type="checkbox" id="about1" name="about"></span></div>CEO Message</label></li> 
</ul> 
</div> 

回答

2

只要用你的分度类检查:

$("#checkall").click(function() { 
    $(this).closest("div.checks").find(":checkbox").attr("checked", this.checked); 
    $(this).closest("div.checks").toggleClass("checked", this.checked); 
}); 

更新

既然你想多checkall输入的,你想要使用一个类而不是一个id。因此,使#checkall => input.checkall,它应该工作。

见我的小提琴:http://jsfiddle.net/c4urself/jvPR3/

+0

不工作.. :(这里http://jsfiddle.net/upWxv/试试吧12/ – Mackelito 2011-12-15 08:10:44

1

你可以使用:

$('#checkall').click(
function(){ 
    $(this).find(':checkbox').attr('checked', this.checked); 
    $(this).find('span').toggleClass('checked', this.checked) 
}) 

<div class="checks"> 
<ul> 
    <li><label for="checkall"><div class="checker"><span class=""><input type="checkbox" id="checkall" name="about"></span></div><strong>About our program</strong></label></li> 
    <li><label for="about1"><div class="checker"><span><input type="checkbox" id="about1" name="about"></span></div>CEO Message</label></li> 
</ul> 
</div> 
+0

不是为我工作:(http://jsfiddle.net/upWxv/6/ – Mackelito 2011-12-15 08:04:59

相关问题