我使用下面的jQuery显示所选计数复选框,显示值
$(document).ready(function(){
$('.check:button').toggle(function(){
$('input:checkbox').attr('checked','checked');
var count = $("input[type=checkbox]:checked").size();
$(this).val('uncheck all')
$("#count").text(count+ " item(s) selected");
},function(){
$('input:checkbox').removeAttr('checked');
var count = $("input[type=checkbox]:checked").size();
$(this).val('check all');
$("#count").text(count+ " item(s) selected");
})
})
这里所选号码复选框是HTML
<input type="button" class="check" value="check all" />
<input type="checkbox" class="cb-element" /> Checkbox 1
<input type="checkbox" class="cb-element" /> Checkbox 2
<input type="checkbox" class="cb-element" /> Checkbox 3
<p id="count"></p>
当我检查所有它显示了正确的数字,就像选择了3个项目,当我取消全选,然后显示选择的项目。
但是,当我删除单个选定的复选框,然后计数不显示。如果我删除一个,那么计数仍然是3,而不是2.
我该如何做到这一点?
jQuery的版本? –
代码是否与您在问题中所描述的完全相同?因为它似乎不工作:http://jsfiddle.net/yDSBs/ –
工作演示 - > http://jsfiddle.net/CSBDS/ –