开发者

Detecting number of checked checkboxes efficiently

hiii, I want to detect how many checkboxes are checked in given list checkboxes. One method that I know is to check it by looping through the list and check one by one if it is checked

$('.chkBoxList').each(function() {
if($(this).is(':checked'))
{
     // do something开发者_开发知识库
}

But I feel it is quite inefficient method. As if there are hundreds/thousands of checkboxes and only few of them are checked, it will still loop through all the checkboxes.

Is there any other way to improve it. I would be grateful if anyone can suggest an efficient alternative to it. Please guide me & suggest appropriate solution with pros & cons.

Thanks in advance.


This will get all checked inputs (assuming there's no any radios) inside element with class chkBoxList and length property will be the count of them:

var cnt = $('.chkBoxList input:checked').length;

If your checkboxes have chkBoxList class use this:

var cnt = $('.chkBoxList:checked').length;


$('.chkBoxList').filter(':checked').length
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜