jquery 复选框问题 - 不检查它是否被禁用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1723649/
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 12:02:17  来源:igfitidea点击:

jquery checkbox issue - do not check if it's disabled

jquerycheckbox

提问by CFNinja

I have 5 checkboxes in each row. The first one is 'ALL'. I am trying to see if any of the others are disabled. So, if somebody clicks on 'ALL' checkbox, I need to make sure the disabled ones are not checked. This is what I have:

我每行有 5 个复选框。第一个是“全部”。我想看看其他人是否被禁用。因此,如果有人单击“全部”复选框,我需要确保未选中禁用的复选框。这就是我所拥有的:

("input[name^=all_]").each(function() {
var input = $(this);   
var name = input.attr('name');    
var num = /\d+$/.exec(name)[0]; 
$(this).click(function() {

if ($('"#G"+num').attr('disabled',false)) {            
$("#G"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ($('"#E"+num').attr('disabled',false)) {         
$("#E"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ($('"#W"+num').attr('disabled',false)) {
$("#W"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ($('"#S"+num').attr('disabled',false)) {
$("#S"+num).attr('checked', $("#all_"+num).is(':checked'));
}
});

});

});

The thing is, the disabled ones still gets checked once I click on 'ALL'. what am i doing wrong? thanks in advance.

问题是,一旦我点击“全部”,残疾的人仍然会被检查。我究竟做错了什么?提前致谢。

回答by CFNinja

this works:

这有效:

 if ( !$("#G"+num).is(':disabled') ) {             
$("#G"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ( !$("#E"+num).is(':disabled')) {            
$("#E"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if ( !$("#W"+num).is(':disabled') ) {
$("#W"+num).attr('checked', $("#all_"+num).is(':checked'));
}
if (!$("#S"+num).is(':disabled')) {
$("#S"+num).attr('checked', $("#all_"+num).is(':checked'));
}
});

回答by Serjas

To checkall except disabled

检查所有的残疾人除外

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

To uncheckall except disabled

取消所有的残疾人除外

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

回答by Doug Domeny

Use jQuery's ":disabled" filter instead of accessing the 'disabled' attribute.

使用 jQuery 的 ":disabled" 过滤器而不是访问 'disabled' 属性。

回答by uji

umm i would probably do this:

嗯,我可能会这样做:


    $('input[name=all]').click(function(){
        var classn = $(this).attr('class');
        $('.'+classn+':not(:disabled)').attr('checked', $(this).is(':checked'));
    });

just assign a uniform class for every row say row_one, row_two and so on.

只需为每一行分配一个统一的类,比如 row_one、row_two 等等。

回答by Rwakos

Try this:

尝试这个:

var all_checks = 0;

$(document).ready(function(){
    $("#all").click(function(){ 
    $('input:checkbox:not(:disabled)').attr("checked",!(all_checks==1));
         if (all_checks == 0){all_checks=1;} else {all_checks = 0;};
    });
});

Where #all is the ID of the master checkbox (which says Check/Uncheck All...

其中#all 是主复选框的 ID(表示选中/取消选中所有...

Hope this helps somebody! Richard Reveron

希望这对某人有帮助!理查德·雷弗龙

回答by cik_imo

I've got the same problem, but I managed to solve it & I think the solution is fine. Here:

我遇到了同样的问题,但我设法解决了它,我认为解决方案很好。这里:

$("INPUT[@name="+children+"][type='checkbox']").attr('checked', $(parent).is(':checked'));
$("INPUT[@name="+children+"][type='checkbox'][disabled]").attr('checked', false);

I think it's easy to understand. No need to explain further, I guess :P But please don't hesitate to ask :)

我认为这很容易理解。无需进一步解释,我想 :P 但请不要犹豫,问 :)

回答by Drew

I know this is an old post, but I had another similar take on it where I wanted to check all check boxes that were not disabled. However, I also have a few other checkboxes that I don't want affected by this, so I needed to target just the <div>that contained them:

我知道这是一篇旧帖子,但我有另一个类似的看法,我想选中所有未禁用的复选框。但是,我还有一些我不想受此影响的其他复选框,因此我只需要定位<div>包含它们的复选框:

$(function () {
    $('#checkAll').click(function () {
        $('#completed_modules').find(':checkbox:not(:disabled)').attr('checked', this.checked);
    });
});

Works like a charm. Enjoy, I hope someone finds this useful.

奇迹般有效。享受,我希望有人觉得这很有用。

回答by Ray

It might be better UI to use a toggle button to "check all/uncheck all" instead of a checkbox for "ALL". So 1 button + 4 checkboxes instead of 5 checkboxes. Then you won't have to mess with disabling.

使用切换按钮“全选/取消全选”而不是“全部”复选框可能是更好的用户界面。所以 1 个按钮 + 4 个复选框而不是 5 个复选框。然后你就不必搞乱禁用了。