我有一个问题.我有一些复选框.我想立刻选择它们,但计算结果是错误的.当如果我使用firefox,歌剧然后确定但是当我使用crome,safari,IE然后它给了我一个错误的结果.为什么?请帮我.
http://jsfiddle.net/Taslimkhan/kdEmH/2/
我在这里设置了一些代码:
// add multiple select / deselect functionality
$("#selectall").click(function () {
$('.case').attr('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
$(".case").click(function(){
if($(".case").length == $(".case:checked").length) {
$("#selectall").attr("checked", "checked");
} else {
$("#selectall").removeAttr("checked");
}
});
});
$(document).ready(function () {
$("input[type=checkbox]").each(function () {
$(this).change(updateCount);
});
updateCount();
function updateCount () {
var count = $("input[type=checkbox]:checked").size();
$("#count").text(count);
$("#status").toggle(count > 0);
};
});
Run Code Online (Sandbox Code Playgroud)
首先,.size()
已弃用.请改用该length
属性.
其次,您可能希望将复选框限制为具有.case
类的复选框:
var count = $("input[type=checkbox].case:checked").length;
Run Code Online (Sandbox Code Playgroud)
第三,你的代码编写方式,你应该调用updateCount()
的click
事件,而不是change
事件,你不需要匿名函数有:
$("input[type=checkbox]").click(updateCount);
Run Code Online (Sandbox Code Playgroud)
我保存了你的jsfiddle的新版本:http://jsfiddle.net/kdEmH/8/