2011-12-15 4 views
1

コードは動作していますが、さらに別のulを追加するとすべてのチェックボックスが選択されます。jQueryは同じ要素内のすべてを選択/選択解除します

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

ちょうどクラスチェックであなたのdivを使用します。

$("#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

+0

http://jsfiddle.net/c4urself/jvPR3/ – c4urself

+0

aaaaah ..ええもちろんもちろん!:Dありがとう! – Mackelito

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

関連する問題