Javascript 单击特定复选框时,取消选中除一个复选框以外的所有复选框

Javascript 单击特定复选框时,取消选中除一个复选框以外的所有复选框,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,假设我有三个复选框,如果我选中了“No Entry”复选框,那么其他复选框都应该被选中。如果我在勾选“No Entry”(无条目)时勾选其他复选框,则应取消勾选“No Entry”(无条目)。我正在尝试使用Javascript和jQuery实现这一点 <input type="checkbox" class="otherCheckboxes" id="checkbox1" value="1" /> 1 <input type="checkbox" class="otherChe

假设我有三个复选框,如果我选中了“No Entry”复选框,那么其他复选框都应该被选中。如果我在勾选“No Entry”(无条目)时勾选其他复选框,则应取消勾选“No Entry”(无条目)。我正在尝试使用Javascript和jQuery实现这一点

<input type="checkbox" class="otherCheckboxes" id="checkbox1" value="1" /> 1
<input type="checkbox" class="otherCheckboxes"  id="checkbox2" value="2" /> 2
<input type="checkbox" id="checkbox3" value="No Entry" /> No Entry

我会这样做:

var othercheckbox=$('.othercheckbox');
var noEntry=$(“#复选框3”);
其他复选框。on('change',函数(e){
如果($(this).is(':checked')){
noEntry.prop('checked',false);
};
}).触发(“变更”);
noEntry.on('change',函数(e){
if(noEntry.is(':checked')){
其他复选框。prop('checked',false);
};
}).触发(“变更”)


1.

2.

禁止入内

我会这样做:

var othercheckbox=$('.othercheckbox');
var noEntry=$(“#复选框3”);
其他复选框。on('change',函数(e){
如果($(this).is(':checked')){
noEntry.prop('checked',false);
};
}).触发(“变更”);
noEntry.on('change',函数(e){
if(noEntry.is(':checked')){
其他复选框。prop('checked',false);
};
}).触发(“变更”)


1.

2.

禁止入内

试试这个:)

或:

试试这个:)

或:


如果您希望禁用其他复选框,那么仅将check属性设置为false是不够的。尝试将disabled设置为true。然后,如果其他复选框被禁用,则用户必须先取消选中“否”项以选择其他复选框。这将增加一次额外的点击,这可能不是一个好主意。@Badhon你能在jQuery中提供一个例子吗。我是新的前端框架的工作。所以这对我来说很困难。如果你想禁用其他复选框,那么仅仅将check属性设置为false是不够的。尝试将disabled设置为true。然后,如果其他复选框被禁用,则用户必须先取消选中“否”项以选择其他复选框。这将增加一次额外的点击,这可能不是一个好主意。@Badhon你能在jQuery中提供一个例子吗。我是新的前端框架的工作。所以这对我来说很困难。非常感谢你的回答。但当我试图实现这一点时,它不起作用。单击功能不起作用:(非常感谢您的回答。但当我尝试实现此功能时,它不起作用。)单击功能不起作用:(这正是我要找的。非常感谢。@Skobalijict这正是我要找的。非常感谢。@Skobalijitch感谢您的回复。非常感谢您的回复。
$('#checkbox3').click(function() {
if ($(this).is(':checked')) {
    $('.otherCheckboxes').attr('checked', false);
}
$('#checkbox3').change(function() {
if (this.checked) {
    $('.otherCheckboxes').each(function(){
        this.checked = false;
    }
}
$('input:checkbox').on('click change',function() {
    if ($(this).prop('checked')) { //if ($(this).is(':checked')) { will do too
        if ($(this).hasClass('otherCheckboxes')) {
            $('#checkbox3').prop('checked',false);
        } else {
            $(this).siblings().prop('checked',false);
        }
    }
});
$('input:checkbox').on('click change',function() {
    if ($(this).is(':checked')) {
        var uncheck = $(this).hasClass('otherCheckboxes')?$('#checkbox3'):$(this).siblings();
        uncheck.prop('checked',false);
    }
});