Select all checkbox elements except disabled ones

后端 未结 8 446
暗喜
暗喜 2021-01-18 09:06

I want to select all checkbox elements expect disabled ones,

this is my html



        
相关标签:
8条回答
  • 2021-01-18 09:21

    It can be as short as

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

    http://jsfiddle.net/hRc4a/

    0 讨论(0)
  • 2021-01-18 09:23
    $('#chkSelectAll').click(function () {
        var checked_status = this.checked;
    
        $('div#item input[type=checkbox]').each(function () {
               if (!this.disabled) 
                   this.checked = checked_status;
        });
    });
    

    or without the each loop :

    $('#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;
        });
    });
    
    0 讨论(0)
  • 2021-01-18 09:28

    Or you may also use the :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 讨论(0)
  • 2021-01-18 09:29

    Check all except disable

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

    Uncheck all except disable

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

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

    0 讨论(0)
  • 2021-01-18 09:32

    I'd personally suggest this solution if you want select all rows except disabled one.

    Just add this code in checkbox input(HTML)

    onclick="$('input[name*=\'selected\']:not(:disabled)').prop('checked',this.checked);"
    
    0 讨论(0)
  • 2021-01-18 09:41

    Use not() to exclude things with a disabled attribute.

    $('#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);
    });
    
    0 讨论(0)
提交回复
热议问题