如何在单击一个复选框时灰显(禁用)所有其他复选框

How to grey out(disable) all other check box on clicking of one check box?

本文关键字:复选框 其他 禁用 一个 单击      更新时间:2023-09-26

例如,我有4个复选框。如果我单击一个复选框,则所有其他复选框都必须变灰(禁用)。。。。

<input type="checkbox" name="box1" >
<input type="checkbox" name="box2" >
<input type="checkbox" name="box3" >

如果你想在取消选择时启用复选框,你可以使用这个:

$('input[type="checkbox"]').click(function() {
    $(this).siblings().attr('disabled', this.checked);
});

JSfiddle

当选中第一个复选框时,这将禁用所有复选框。主要用于前端的"全选"。

HTML:

<input type="checkbox" name="box1" id="gowithit"> All
<input type="checkbox" name="box2" > Red
<input type="checkbox" name="box3" > Blue
<input type="checkbox" name="box4" > Green

Javascript:

$('#gowithit').click(function() {
    $(this).siblings().attr('disabled', this.checked);
});