如何计算每个选中的复选框 [英] How to count every checked checkboxes
问题描述
这里是我的代码:
它实际上是计数复选框,并写在< span class =counter> / span>
。此代码适用于Firefox,但不适用于Chrome。
It actually count checked checkboxes and write it inside <span class="counter"></span>
. This code works on Firefox, but not on Chrome.
在Chrome上,.select_all选中所有复选框,但不更新计数器。
On Chrome, the .select_all check all checkboxes I want, but doesn't update the counter. Actually counter get updated when I uncheck the .select_all, which is weird.
重要事实:我不想计数.counter
IMPORTANT FACT: I don't want to count the .Select_all checkboxes inside my .counter
jQuery(document).ready(function($){
$(function() {
$('#general i .counter').text(' ');
var generallen = $("#general-content input[name='wpmm[]']:checked").length;
if(generallen>0){$("#general i .counter").text('('+generallen+')');}else{$("#general i .counter").text(' ');}
})
$("#general-content input:checkbox").on("change", function() {
var len = $("#general-content input[name='wpmm[]']:checked").length;
if(len>0){$("#general i .counter").text('('+len+')');}else{$("#general i .counter").text(' ');}
});
$(function() {
$('.select_all').change(function() {
var checkthis = $(this);
var checkboxes = $(this).parent().next('ul').find("input[name='wpmm[]']");
if(checkthis.is(':checked')) {
checkboxes.attr('checked', true);
} else {
checkboxes.attr('checked', false);
}
});
});
});
EDIT:以下是代码的示例文档: http://jsfiddle.net/8PVDy/1/
Here is a example document of the code : http://jsfiddle.net/8PVDy/1/
推荐答案
您可以使用函数更新计数器:
You can use a function to update the counter :
function updateCounter() {
var len = $("#general-content input[name='wpmm[]']:checked").length;
if(len>0){$("#general i .counter").text('('+len+')');}else{$("#general i .counter").text(' ');}
}
并在复选框的状态改变时调用此函数包括selectAll复选框)
and call this function when a checkbox's state is changed (including the selectAll checkboxes)
以下是更新的jsFiddle: http:// jsfiddle .net / 8PVDy / 4 /
Here is an updated jsFiddle : http://jsfiddle.net/8PVDy/4/
这篇关于如何计算每个选中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!