jQuery 如何计算选中了多少个复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11861024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:00:53  来源:igfitidea点击:

How to count how many checkboxes has been checked

jquery

提问by Code Ratchet

I have a HTML table first column consists of check boxes etc when the user clicks a button i want to check if any of the check boxes have been checked before going to the code behind etc.

我有一个 HTML 表格,第一列由复选框等组成,当用户单击一个按钮时,我想在转到后面的代码等之前检查是否已选中任何复选框。

This is what i have but it keeps throwing an error "Microsoft JScript runtime error: Syntax error, unrecognized expression: input.checkbox:checked"]"

这就是我所拥有的,但它不断抛出错误“Microsoft JScript 运行时错误:语法错误,无法识别的表达式:input.checkbox:checked”]”

This is my code i just want to return the count of how many checkboxes are actually checked.

这是我的代码,我只想返回实际选中的复选框数。

   $('#BtnExportToExcel').click(function () {
          var check = ('#gvPerformanceResult').find('input.checkbox:checked"]').length;
          alert(check);
    return false;
    }):
   $('#BtnExportToExcel').click(function () {
          var check = ('#gvPerformanceResult').find('input.checkbox:checked"]').length;
          alert(check);
    return false;
    }):

Thanks for your help.

谢谢你的帮助。

回答by Curt

You have invalid syntax (No $at the start, and "]at the end).

您的语法无效($开头和"]结尾均为否)。

Change:

改变:

('#gvPerformanceResult').find('input.checkbox:checked"]').length;

To:

到:

$('#gvPerformanceResult').find('input.checkbox:checked').length;

回答by Chris

You have a dangling bracket in the find()function, try this

你在find()函数中有一个悬空括号,试试这个

$('#BtnExportToExcel').click(function () {
   var check = $('#gvPerformanceResult').find('input[type=checkbox]:checked').length;
   alert(check);
   return false;
}):

Note, based on your answer it seems you want ALL checkboxes within #gvPerformanceResultand not checkboxes with the class .checkbox- use [type=checkbox]to select those checkboxes

请注意,根据您的回答,您似乎希望所有复选框都在类中,#gvPerformanceResult而不是类中的复选框.checkbox- 用于[type=checkbox]选择这些复选框

回答by Andrea Turri

$('input[type="checkbox"]:checked').length;

This will return the number of checked checkbox.

这将返回选中复选框的数量。



Restrict:

严格:

$('#gvPerformanceResult').find('input[type="checkbox"]:checked').length;

回答by Code Ratchet

Figured it out

弄清楚了

   $('#BtnExportToExcel').click(function () {
                var check = $("input:checkbox:checked").length
                alert(check);
});
   $('#BtnExportToExcel').click(function () {
                var check = $("input:checkbox:checked").length
                alert(check);
});