开发者

How to check if all checkboxes inside a div are checked using jQuery?

开发者 https://www.devze.com 2023-04-04 00:55 出处:网络
this fails: var all_checkboxes = $(\'#myDiv input[type=\"checkbox\"]\'); if (all_checkboxes.is(\':checked\')) {

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
0

精彩评论

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