简体   繁体   English

禁用除一个复选框外的所有复选框

[英]Disable all but one checkbox

I saw this on jsfiddle and I wanted to know how I would alter it so that if and only if 45 is checked the rest are disabled, if if 45 is unselected then the rest are enabled. 在jsfiddle上看到了这一点 ,我想知道如何更改它,以便只有在选中45的情况下,其余的才被禁用,如果未选择45,则其余的都将被启用。 I should be able to do all this with out submitting the form. 我无需提交表格就能完成所有这些工作。

The example javascript on jsfiddle: jsfiddle上的示例javascript:

$(function(){
    $(".rambo").change(function(){
        $(this).siblings().attr("disabled", $(this).is(":checked"));  
    });
});

You can use attribute selector . 您可以使用attribute selector

$(function(){
    $(".rambo[value=1]").change(function(){
        $(this).siblings().prop("disabled", this.checked);  
    });
});

http://jsfiddle.net/Mnmte/ http://jsfiddle.net/Mnmte/

从其他人中删除.rambo类。

$(function(){
    $(".rambo").change(function(){
      if($(this).val() == 1){
        $(this).siblings().attr("disabled", $(this).is(":checked"));  
      }
    });
});

One way 单程

Above from all the answers, you can also specify that you only want the 1st checkbox to execute the script by using nth-child selector: 在所有答案的上方,您还可以使用nth-child选择器指定只希望第一个checkbox执行脚本:

$(".rambo:nth-child(1)").change(function(){
    $(this).siblings().attr("disabled", $(this).is(":checked"));  
});

Demo 演示版

Just add proper id s and select on that 只需添加适当的id并选择

HTML: HTML:

<input type="checkbox" id="cb1" class="rambo" value='1' /> 45 
<input type="checkbox" id="cb2" class="rambo" value='2' /> 56 
<input type="checkbox" id="cb3" class="rambo" value='3' /> 56

JS: JS:

$(function(){
    $("#cb1").change(function(){
        $(this).siblings().attr("disabled", $(this).is(":checked"));  
    });
});

JSFiddle JSFiddle

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

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