Javascript 单击具有类似类的复选框时禁用其他复选框

Javascript 单击具有类似类的复选框时禁用其他复选框,javascript,jquery,html,Javascript,Jquery,Html,一旦其中一个复选框被选中,我就需要禁用同一类的其余复选框 $('.Cat .RoleChk').change(function(){ if($(this).is(':checked')){ $('.Cat .RoleChk').attr('disabled',true); $(this).attr('disabled',''); } else{ $('.Cat .RoleChk').attr('disabled',''); } }); 员工角色 办公室职员 营销人员 项目经

一旦其中一个复选框被选中,我就需要禁用同一类的其余复选框

$('.Cat .RoleChk').change(function(){
    if($(this).is(':checked')){
$('.Cat .RoleChk').attr('disabled',true);
    $(this).attr('disabled','');
}
else{
$('.Cat .RoleChk').attr('disabled','');
}
});


员工角色

办公室职员
营销人员
项目经理
团体

航空
电气
采矿
那么:

$(".RoleChk, .GrpChk").change(function() {
    this.checked ? $("." + this.className).not(this).prop("disabled", true) : $("." + this.className).not(this).prop("disabled", false);
});
演示:

我将选中的原始复选框保持为启用状态,这允许用户取消选中并重新启用。如果要删除此功能,请将
.not(this)
从三元组中取出。


<script>
jQuery(document).ready(function(){
    $('.Cat .RoleChk').change(function(){
        if($(this).is(':checked')){
            $('.Cat .RoleChk').attr('disabled',true);
            $(this).removeAttr("disabled");
        }
        else{
            $('.Cat .RoleChk').removeAttr("disabled");
        }
    });
});
</script>
jQuery(文档).ready(函数(){ $('.Cat.RoleChk').change(函数(){ 如果($(this).is(':checked')){ $('Cat.RoleChk').attr('disabled',true); $(此).removeAttr(“禁用”); } 否则{ $('.Cat.RoleChk').removeAttr(“禁用”); } }); });
Wow,这可以压缩吗?太棒了!我喜欢用单选按钮来做这样的事情
<script>
jQuery(document).ready(function(){
    $('.Cat .RoleChk').change(function(){
        if($(this).is(':checked')){
            $('.Cat .RoleChk').attr('disabled',true);
            $(this).removeAttr("disabled");
        }
        else{
            $('.Cat .RoleChk').removeAttr("disabled");
        }
    });
});
</script>