添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

$(document).ready(function(){ // 1
    $(':checkbox.selectall').on('click', function(){
        $(':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
    $(':checkbox.selectall').on('click', function(){
        $(':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/

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

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

$(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");

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

$(':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:

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

I hope it helps :)

Hm... Did you read the question? This only does part of what the original code did. There is nothing in there to handle the disabled checkboxes. – Guffa Jul 12, 2012 at 17:23 Sorry I forgot to include that in the code, I just edited my answer adding :not(:disabled)... – sergiocruz Jul 12, 2012 at 17:33

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");
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.