Count the number of checked checkboxes in HTML

后端 未结 6 904
一整个雨季
一整个雨季 2020-12-10 01:13

So basically i want to count the number of checkboxes that are ticked. I get my code to the point where it counts them successfully, but I want to put in an alert that shows

6条回答
  •  长情又很酷
    2020-12-10 01:47

    Thanks to Marlon Bernardes for this. alert(document.querySelectorAll('input[type="checkbox"]:checked').length);

    If you have more than one form with different checkbox names in each, the above code will count all checkboxes in all forms.

    To get over this, you can modify it to isolate by name.

    var le = document.querySelectorAll('input[name="chkboxes[]"]:checked').length;
    

提交回复
热议问题