2011-05-18 58 views
3
<ul class="sorting" id="filter_by"> 
    <li><input checked="checked" type="checkbox" id="foobar" /><label for="chk1">foobar</label></li> 
    <li><input checked="checked" type="checkbox" id="foobaz"/><label for="chk2">baz</label></li> 
    <li><input checked="checked" type="checkbox" id="foofoo"/><label for="chk3">foo</label></li> 
</ul> 

我想知道有多少复选框被选中。所以我写道:chekbox点击创建问题?

$("#filter_by").change(function() { 
    alert('hi'); 
    var len_checked = $("#filter_by > li > input:checked").length; 
}); 

但每次它要求两次。所以,警觉跑两次。假设首先检查所有三个 现在我取消选中了一个元素。然后警报运行两次。我能找到原因?

回答

3

您应该.change处理程序绑定到复选框,而不是UL:

$("#filter_by :checkbox").change(function() { 
    alert('hi'); 
    var len_checked = $("#filter_by > li > input:checked").length; 
}); 

You can try it here.

这么说,我不能重复你报道什么,至少不会在Chrome的代码你发布了,所以别的东西可能会导致这种情况。你有没有相关的细节?

+0

我包括我的js文件两次。 BTW谢谢.. – 2011-05-18 10:17:35