6

我想选择除checkbox禁用元素之外的所有元素,

这是我的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;
    });
})

它适用于选择所有checkbox元素,但我想跳过禁用的元素。

我怎样才能做到这一点?

4

8 回答 8

17

使用 not() 排除具有禁用属性的事物。

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;

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

更简洁

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;
    $('div#item input[type=checkbox]').not(":disabled").prop("checked", checked_status);
});
于 2013-05-08T19:15:23.663 回答
3

它可以短至

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

http://jsfiddle.net/hRc4a/

于 2013-05-08T19:25:18.740 回答
2
$('#chkSelectAll').click(function () {
    var checked_status = this.checked;

    $('div#item input[type=checkbox]').each(function () {
           if (!this.disabled) 
               this.checked = checked_status;
    });
});

或没有每个循环:

$('#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;
    });
});
于 2013-05-08T19:14:16.290 回答
0
    $('#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;
             }

        });
    })
于 2013-05-08T19:17:28.937 回答
0

或者您也可以使用 :not 选择器,如下所示:

$('#chkSelectAll').click(function () {
    var checked_status = this.checked;
    $('div#item input[type=checkbox]:not(:disabled)').each(function () {
               this.checked = checked_status;
    });
});
于 2013-05-08T19:21:35.377 回答
0

我个人建议:

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

JS 小提琴演示

参考:

于 2013-05-08T19:31:20.330 回答
0

检查除禁用之外的所有内容

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

取消选中除禁用之外的所有

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

有关详细信息,请参阅以下链接http://www.infinetsoft.com/Post/How-to-check-all-except-disabled/18#.V00SYfl97IU

于 2016-05-31T04:30:15.017 回答
0

如果您想选择除禁用行之外的所有行,我个人建议使用此解决方案。

只需在复选框输入中添加此代码(HTML)

onclick="$('input[name*=\'selected\']:not(:disabled)').prop('checked',this.checked);"
于 2018-07-18T05:09:37.937 回答