简体   繁体   中英

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. I should be able to do all this with out submitting the form.

The example javascript on jsfiddle:

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

You can use attribute selector .

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

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:

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

Demo

Just add proper id s and select on that

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:

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

JSFiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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