Select all checkboxes except disabled ones

I want to select all the checkbox items that are waiting for disconnected,

this is my html

 <input id="chkSelectAll" class="checkbox" type="checkbox" />Select All <div id="ContentPlaceHolder1_listItem_pnlDis_0"> <input id="checkApproved" type="checkbox" name="checkApproved" checked="checked" disabled="disabled"> </div> <div id="ContentPlaceHolder1_listItem_pnlDis_8" class="dis"> <input id="checkApproved" type="checkbox" name="ctl00$ContentPlaceHolder1$listItem$ctrl8$checkApproved"> </div> 

JQuery

 $('#chkSelectAll').click(function () { var checked_status = this.checked; // alert(checked_status); $('div#item input[type=checkbox]').each(function () { this.checked = checked_status; }); }) 

It works to select all checkbox items, but I want to skip disabled ones.

How can i do this?

+4
source share
7 answers

Use not () to exclude objects with the attribute disabled.

 $('#chkSelectAll').click(function () { var checked_status = this.checked; $('div#item input[type=checkbox]').not("[disabled]").each(function () { this.checked = checked_status; }); }); 

more concise

 $('#chkSelectAll').click(function () { var checked_status = this.checked; $('div#item input[type=checkbox]').not(":disabled").prop("checked", checked_status); }); 
+13
source
 $('#chkSelectAll').click(function () { var checked_status = this.checked; $('div#item input[type=checkbox]').each(function () { if (!this.disabled) this.checked = checked_status; }); }); 

or without each cycle:

 $('#chkSelectAll').on('click', function() { var checked_status = this.checked; $('div#item input[type=checkbox]').prop('checked', function(i, prop) { return this.disabled ? prop : checked_status; }); }); 
+2
source

It may be shorter

 $('#chkSelectAll').click(function() { $('div#item :checkbox:not(:disabled)').prop('checked', this.checked); }); 

http://jsfiddle.net/hRc4a/

+2
source
  $('#chkSelectAll').click(function () { var checked_status = this.checked; // alert(checked_status); $('div#item input[type=checkbox]').each(function () { if(!$(this).is(':disabled'){ this.checked = checked_status; } }); }) 
0
source

Or you can also use: not selector as follows:

 $('#chkSelectAll').click(function () { var checked_status = this.checked; $('div#item input[type=checkbox]:not(:disabled)').each(function () { this.checked = checked_status; }); }); 
0
source

I personally suggested:

 $('#chkSelectAll').change(function(){ var self = this; $('input:checkbox').filter(function(){ return !this.disabled }).prop('checked',self.checked); }); 

JS Fiddle demo .

Literature:

0
source

Check everything except disable

 $('input:checkbox:not(:disabled)').attr('checked', 'checked'); 

Uncheck all but disable

 $('input:checkbox:not(:disabled)').removeAttr('checked'); 

see the link below for more details http://www.infinetsoft.com/Post/How-to-check-all-except-disabled/18#.V00SYfl97IU

0
source

Source: https://habr.com/ru/post/1479809/


All Articles