繁体   English   中英

如果我选择第一个复选框,则禁用所有剩余的复选框

[英]If I select first checkbox then disable all remaining checkboxes

我在单独的标签中有四个复选框。

如果我选择第一个复选框,则所有其余复选框将被禁用。 如果再次单击第一个 ,将启用所有复选框。 如果我选择任何其他复选框,则其他复选框仍处于启用状态:我可以选择多个复选框。 在这里查看图片

我该如何设置?

如果要询问如何禁用所有其他“复选框”,请使用无线电组。 如果您说第一个复选框会自动触发其他复选框的切换,请尝试将它们取消分组,或者将它们彼此分开,因为这听起来好像您在这里存在某种错误的分组和依赖性。

尝试这个:

HTML:

<label for="one"><input type="checkbox" name="one" id="one"></label>
<label for="two"><input type="checkbox" name="two" id="two"></label>
<label for="three"><input type="checkbox" name="three" id="three"></label>
<label for="four"><input type="checkbox" name="four" id="four"></label>

jQuery的:

$('input[type="checkbox"]').on('change', function() {
    if($('input[type="checkbox"]').eq(0).is(':checked')){
        $('input[type="checkbox"]').not($(this)).attr('disabled', true);
    }
    else{
        $('input[type="checkbox"]').not($(this)).attr('disabled', false);
    }
});

工作示例: https//jsfiddle.net/e26vnnk2/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM