简体   繁体   English

尝试禁用单选按钮

[英]Trying to disable radio buttons

I have two radio buttons to select priority as high or regular. 我有两个单选按钮可以将优先级选择为高或常规。

I wrote this function to disable the radiobox which was not selected. 我写了这个功能来禁用未选中的单选框。 For some reason it won't disable the box though. 由于某种原因,它不会禁用该框。 I get no error in the consol. 我在consol中没有任何错误。

function disablePriority(){
    var reg = $('input[value="ct100"]');
    var high = $('input[value="ct101"]');

    alert('cp');
    if(reg.attr("checked")){
    alert('cp1');
        high.attr("disabled","disabled");
    }else{
    alert('cp2');
        reg.attr("disabled","disabled");
    }
}

This is the html for the two radio boxes 这是两个单选框的html

<input id="ctl00_m_g_72c1058f_372a_4780_b0c6_58f4c7012b35_ff61_ctl00_ctl00" type="radio" name="ctl00$m$g_72c1058f_372a_4780_b0c6_58f4c7012b35$ff61$ctl00$RadioButtons" value="ctl00">


<input id="ctl00_m_g_72c1058f_372a_4780_b0c6_58f4c7012b35_ff61_ctl00_ctl01" type="radio" name="ctl00$m$g_72c1058f_372a_4780_b0c6_58f4c7012b35$ff61$ctl00$RadioButtons" value="ctl01" checked="checked">

Try with disabled as true like 尝试disabledtrue

if(reg.attr("checked")){

    high.attr("disabled",true);
}else{

    reg.attr("disabled",true);
}

CHeck this FIDDLE 检查这个FIDDLE

high.attr("disabled",'disabled'); or high.prop("disabled",'true');


high[0].disabled=true;high[1].disabled=true


high.removeAttr("disabled"); //to remove it

Ok, first there is a typo in your input value . 好的,首先您的输入value有一个错字。 In the HTML it is ctl01 and ctl00 (note the small-case 'L'). 在HTML中,它是ctl01和ctl00(请注意小写的“ L”)。 In your jQuery it is ct100 and ct101. 在您的jQuery中,它是ct100和ct101。

Change the value as below, it works. 如下更改值,它可以工作。

var reg = $('input[value="ctl00"]');
var high = $('input[value="ctl01"]');

if(reg.prop("checked")){
alert('cp1');
    high.prop("disabled",true);
}else{
alert('cp2');
    reg.prop("disabled", true);
}

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

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