Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 选中除禁用元素外的所有复选框元素_Jquery - Fatal编程技术网

Jquery 选中除禁用元素外的所有复选框元素

Jquery 选中除禁用元素外的所有复选框元素,jquery,Jquery,我想选中所有复选框元素,除了禁用的元素 这是我的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"

我想选中所有
复选框
元素,除了禁用的元素

这是我的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>
它用于选择所有
复选框
元素,但我想跳过禁用的元素

我该怎么做

$('#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;
    });
});
使用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);
});

或者您也可以使用: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() {
    $('div#item :checkbox:not(:disabled)').prop('checked', this.checked);
});

我个人建议:

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

参考资料:

检查除禁用之外的所有选项 取消选中除禁用之外的所有选项
有关更多详细信息,请参阅下面的链接

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

只需将此代码添加到复选框输入(HTML)


$('div#item input[type=checkbox]:not(:checked)
可能重复的@sizz:您不需要
。每个
。您也可以只执行
$('input[type=checkbox]')。不(“:disabled”).prop('checked',checked_status)-更常见的使用选择器的方法可能不是(“:disabled”)。我假设运行
filter
方法比简单地使用
not
选择器要慢得多。@FrançoisWahl:奇怪的是,我没有考虑到一点:该死,现在我必须去JS Perf in'进行测试。(嗯,不管怎样,晚饭后……)我也在想同样的事情:我为每个排列添加了检查和取消检查。不确定它是否写得足够好,但可以尝试一下,看看你是否可以改进它。我认为
filter
之所以较慢,是因为它首先对所有内容进行收集,然后对所有内容进行迭代以排除禁用的内容。而
not
选择器不会选择/返回禁用的选项。我想,如果可能的话,只返回您需要开始的内容也是有意义的。
$('#chkSelectAll').click(function() {
    $('div#item :checkbox:not(:disabled)').prop('checked', this.checked);
});
$('#chkSelectAll').change(function(){
    var self = this;
    $('input:checkbox').filter(function(){
        return !this.disabled
    }).prop('checked',self.checked);
});
$('input:checkbox:not(:disabled)').attr('checked', 'checked');
$('input:checkbox:not(:disabled)').removeAttr('checked');
onclick="$('input[name*=\'selected\']:not(:disabled)').prop('checked',this.checked);"