Check if checkbox is checked with jQuery

后端 未结 23 3371
忘了有多久
忘了有多久 2020-11-21 23:12

How can I check if a checkbox in a checkbox array is checked using the id of the checkbox array?

I am using the following code, but it always returns the count of ch

23条回答
  •  轮回少年
    2020-11-21 23:43

    $('#' + id).is(":checked")
    

    That gets if the checkbox is checked.

    For an array of checkboxes with the same name you can get the list of checked ones by:

    var $boxes = $('input[name=thename]:checked');
    

    Then to loop through them and see what's checked you can do:

    $boxes.each(function(){
        // Do stuff here with this
    });
    

    To find how many are checked you can do:

    $boxes.length;
    

提交回复
热议问题