How to check if all checkboxes inside a div are checked using jQuery?
this fails:
var all_checkboxes = $('#myDiv input[type="checkbox"]');
if (all_checkboxes.is(':checked')) {
alert('they are all checked');
}
it ale开发者_开发知识库rts they are all checked even if just one and not all is checked.
you can check if all your checkboxes are checked like this:
var all_checkboxes = $('#myDiv input[type="checkbox"]');
if (all_checkboxes.length === all_checkboxes.filter(":checked").length) {
alert('they are all checked');
}
Check that none of them are unchecked:
if (all_checkboxes.not(":checked").length === 0)
Just add a same class "myClass" to all the check boxes and then:
let areAllChecked = !$('.myClass').not(':checked').length; //'true' if all checked
精彩评论