2013-08-23 42 views
0

我有兩個單選按鈕來選擇優先級爲高或常規。試圖禁用單選按鈕

我寫了這個功能來禁用未被選中的radiobox。由於某些原因,它不會禁用該框。我在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"); 
    } 
} 

這是兩個單選框的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"> 

回答

0

好的,首先你的輸入value有一個錯字。在HTML中,它是ctl01和ctl00(注意小寫'L')。在你的jQuery中是ct100和ct101。

更改值如下,它的工作原理。

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); 
} 
+0

哇。謝謝你。現在工作正常。 – Batman

0

disabled嘗試爲true

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

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

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

檢查這個FIDDLE

+0

它不工作.. ?? – Gautam3164

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


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


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