如果未禁用,则 Jquery 全选

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11457050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:37:26  来源:igfitidea点击:

Jquery select all if not disabled

jqueryselectcheckbox

提问by user1488434

I am using the following script to select all checkboxes with a given class.

我正在使用以下脚本选择具有给定类的所有复选框。

$(document).ready(function(){ // 1
    // 2
    $(':checkbox.selectall').on('click', function(){
        // 3
        $(':checkbox[class='+ $(this).data('checkbox-name') + ']').prop("checked", $(this).prop("checked"));
        $(':checkbox[class='+ $(this).data('checkbox-name') + ']').trigger("change");
    });

});

However I'm having a problem as the de/select all checkbox is able to de/select checkboxes which are disabled.

但是我遇到了一个问题,因为取消/全选复选框能够取消/选择被禁用的复选框。

I tried this

我试过这个

$(document).ready(function(){ // 1
    // 2
    $(':checkbox.selectall').on('click', function(){
        // 3
        $(':checkbox[class='+ $(this).data('checkbox-name') + !$(:disabled) + ']').prop("checked", $(this).prop("checked"));
        $(':checkbox[class='+ $(this).data('checkbox-name') + !$(:disabled) + ']').trigger("change");
    });

});

But it does not work. I have made a jsfiddle to showcase the problem http://jsfiddle.net/e67Fv/

但它不起作用。我做了一个 jsfiddle 来展示这个问题http://jsfiddle.net/e67Fv/

回答by Guffa

Hm... interresting attempt, but you can't use a jQuery object inside a selector, as the selector is just a plain string.

嗯...有趣的尝试,但您不能在选择器中使用 jQuery 对象,因为选择器只是一个普通字符串。

The selector for excluding the disabled elements would be :not(:disabled), so your code should be:

排除禁用元素的选择器将是:not(:disabled),因此您的代码应该是:

$(document).ready(function(){
  $(':checkbox.selectall').on('click', function(){
    $(':checkbox[class='+ $(this).data('checkbox-name') + ']:not(:disabled)').prop("checked", $(this).prop("checked"));
    $(':checkbox[class='+ $(this).data('checkbox-name') + ']:not(:disabled)').trigger("change");
  });
});

Note that you can chain calls, so you don't have to select the items twice:

请注意,您可以链接调用,因此您不必选择项目两次:

$(document).ready(function(){
  $(':checkbox.selectall').on('click', function(){
    $(':checkbox[class='+ $(this).data('checkbox-name') + ']:not(:disabled)').prop("checked", $(this).prop("checked")).trigger("change");
  });
});

回答by Goran Mottram

Use a combonation of the .not()function and :disabledselector to exclude these.

使用.not()函数和:disabled选择器的组合来排除这些。

$(':checkbox[class='+ $(this).data('checkbox-name') + ']').not(':disabled').prop("checked", $(this).prop("checked"));
$(':checkbox[class='+ $(this).data('checkbox-name') + ']').not(':disabled').trigger("change");

.not()also exists as a selector as :not()and could be used as follows:

.not()也作为选择器存在:not(),可以如下使用:

 $(':checkbox[class='+ $(this).data('checkbox-name') + ']:not(:disabled)').prop("checked", $(this).prop("checked"));
 $(':checkbox[class='+ $(this).data('checkbox-name') + ']:not(:disabled)').trigger("change");

回答by sergiocruz

Here is what I usually do:

这是我通常做的:

$(function(){
    $(".selectall").live('change', function(){
        if($(this).is(":checked"))
        {
            $("input:checkbox:not(:disabled)." + $(this).data('checkbox-name')).prop("checked", "true");
        }
        else
        {
            $("input:checkbox:not(:disabled)." + $(this).data('checkbox-name')).prop("checked", "false");
        }
    });
});

I hope it helps :)

我希望它有帮助:)

回答by Rune FS

This does not do the same as your original code. It would only trigger change for those that was not changed. I've assummed you wished to trigger change for all the once you changed

这与您的原始代码不同。它只会触发那些没有改变的改变。我假设您希望在更改后触发所有更改

$(':checkbox.selectall').on('click', function(){
        $(':checkbox .'+ $(this).data('checkbox-name')).not(':disabled').prop("checked", $(this).prop("checked")).trigger("change");
    });