开发者

jquery get checked checkboxes

开发者 https://www.devze.com 2023-03-02 19:51 出处:网络
i\'m trying to get the list of checkboxes and the count that are checked. I have this: var obj = $(this).closest(\'li\').find(\':checkbox\');

i'm trying to get the list of checkboxes and the count that are checked. I have this:

        var obj = $(this).closest('li').find(':checkbox');

        var childCount=$(obj).size();
        var checkedCount=$(obj).(':checked').length;

I get error on checkedCount开发者_开发问答

??


You need to use the filter() function:

    var obj = $(this).closest('li').find(':checkbox');

    var childCount = obj.size();
    var checkedCount =  obj.filter(':checked').length;

filter
Reduce the set of matched elements to those that match the selector or pass the function's test.

Also, you don't need to wrap obj with $(), because it's already a jQuery object.


to get checked checkboxes length :

$('input[name^="complete"]:checked').length;

to get unchecked checkboxes length :

$('input[name^="complete"]:unchecked').length;

where "complete" is a name attribute.


you have a typo .. you can't do $('#foo').() .. you need:

$('#foo').filter(':checkbox') 

filter selector - http://api.jquery.com/filter

0

精彩评论

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