简体   繁体   中英

How to count the number of checked checkboxes using javascript?

I have a list of checkboxes for all members that subscribe to a mailing list. I am able to check a number of members that I wish to send to. How can I count the number of Checked checkboxes?

    $('#lblCheck').html(checkedCheckBoxes)
    var checkedCheckBoxes = $("[type='checkbox']:checked").length;

The order was wrong, your code should work if you fix it.

var checkedCheckBoxes = $("[type='checkbox']:checked").length;  
$('#lblCheck').html(checkedCheckBoxes)

Please try the below. It's plain javascript. It shall work.

let countCheckboxes = function () {
let noOfCheckboxes = 0;
for(let i=0; i <inputTypes.length; i++) {
    if (inputTypes[i].getAttribute('type') === 'checkbox') {
        noOfCheckboxes++;
    }
}
return noOfCheckboxes;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM