如何计算已选中多少个复选框 [英] How to count how many checkboxes has been checked

查看:80
本文介绍了如何计算已选中多少个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击按钮时,我有一个HTML表第一列,其中包括复选框,我想在转到后面的代码之前检查是否已选中任何复选框.

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

这是我的代码,我只想返回实际选中了多少个复选框的计数.

感谢您的帮助.

解决方案

find()函数中有一个悬挂的括号,请尝试

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

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

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.

This is what i have but it keeps throwing an error "Microsoft JScript runtime error: Syntax error, unrecognized expression: 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;
    }):

Thanks for your help.

解决方案

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

$('#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 #gvPerformanceResult and not checkboxes with the class .checkbox - use [type=checkbox] to select those checkboxes

这篇关于如何计算已选中多少个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆