简体   繁体   English

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

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

I have four check boxes inside a separate label. 我在单独的标签中有四个复选框。

If I select first check box then all remaining check boxes will be disable. 如果我选择第一个复选框,则所有其余复选框将被禁用。 If I click first again, all check boxes will be enabled. 如果再次单击第一个 ,将启用所有复选框。 If I select any other check box, the others are still enabled: I can select more than one. 如果我选择任何其他复选框,则其他复选框仍处于启用状态:我可以选择多个复选框。 See image here 在这里查看图片

How can I set this up? 我该如何设置?

If you are asking in order to find out how to disable all other "checkboxes," use radiogroups. 如果要询问如何禁用所有其他“复选框”,请使用无线电组。 If you are saying that the first checkbox auto-triggers a toggle on the other checkboxes, try ungrouping them, or making them separate in and of themselves, because it sounds as if you have some kind of faulty grouping and dependency there. 如果您说第一个复选框会自动触发其他复选框的切换,请尝试将它们取消分组,或者将它们彼此分开,因为这听起来好像您在这里存在某种错误的分组和依赖性。

Try this: 尝试这个:

HTML: 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: 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);
    }
});

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

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

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