2011-09-23 104 views
32

我想要有與單選按鈕相關的文本框。因此,每個單選按鈕應該啓用它的文本框並禁用其他按鈕。但是,當我將textbox的禁用屬性設置爲true時,它也會更改可編輯屬性。我試圖再次設置可編輯屬性true,但它不起作用。.setAttribute(「disabled」,false);將可修改的屬性更改爲false

這是我的嘗試:

JS功能:

function enable(id) 
{ 
    var eleman = document.getElementById(id); 
    eleman.setAttribute("disabled", false); 
    eleman.setAttribute("editable", true); 
} 

XUL元素:

<radio id="pno" label="123" onclick="enable('ad')" /> 
<textbox id="ad" editable="true" disabled="true" flex="1" emptytext="asd" onkeypress="asd(event)" tooltiptext="" > 

回答

70

一個disabled元件是,(自我解釋)禁用,從而在邏輯上不可編輯。所以:

集殘疾人屬性[...]改變編輯屬性太

預期的良好定義的行爲。

這裏真正的問題似乎是你想設置disabledfalse通過setAttribute()這不會做你期待的。如果設置了屬性disabled,則該元素將被禁用,與其值無關(因此,disabled="true"disabled="disabled"disabled="false"全都相同:元素被禁用)。你應該改爲移除完整的屬性:

element.removeAttribute("disabled"); 

或設置該屬性直接:

element.disabled = false; 
+1

好的答案,但在後一種情況下,你說「直接設置該屬性」,我相信它應該是「直接設置該屬性」,因爲您正在編輯DOM屬性而不是HTML屬性。細微的差異,但重要的是我認爲。 – The111

+1

@ The111:謝謝你的提示,我改變了。 – oezi

+0

有什麼辦法可以使用'setAttribute()'做同樣的事情嗎? 'setAttribute(「disabled」,「true」);'不起作用; –

6

儘量不要做這樣的:

function enable(id) 
{ 
    var eleman = document.getElementById(id); 
    eleman.removeAttribute("disabled");   
} 

要啓用一個元素你必須刪除禁用的屬性。將其設置爲false仍然意味着它被禁用。

http://jsfiddle.net/SRK2c/

3

殘疾人屬性值actally沒有考慮。通常,如果你已經注意到了屬性被設置爲禁用=「禁用」「已禁用」這裏是沒有必要的PerSay的..因此,最好的事情做的是刪除該屬性。

element.removeAttribute("disabled");  

還你可以做

element.disabled=false; 
9

就直接設置屬性。

eleman.disabled = false; 
+0

有什麼辦法,我可以使用的setAttribute()做同樣的事情?的setAttribute( 「禁用」, 「真」);不起作用 –

+1

@ AjayAradhya-看到接受的答案。 * disabled *屬性是布爾值,它的存在將其值設置爲true。解決這個問題的唯一方法是將屬性設置爲false或使用* removeAttribute *刪除屬性。 – RobG

0

只需更換 'myselect' 用你的身份證

到禁止 - >

document.getElementById("mySelect").disabled = true; 

啓用 - >

document.getElementById("mySelect").disabled = false; 
0

使用方法設置刪除屬性

function radioButton(o) { 
 

 
    var text = document.querySelector("textarea"); 
 

 
    if (o.value == "on") { 
 
    text.removeAttribute("disabled", ""); 
 
    text.setAttribute("enabled", ""); 
 
    } else { 
 
    text.removeAttribute("enabled", ""); 
 
    text.setAttribute("disabled", ""); 
 
    } 
 
    
 
}
<input type="radio" name="radioButton" value="on" onclick = "radioButton(this)" />Enable 
 
<input type="radio" name="radioButton" value="off" onclick = "radioButton(this)" />Disabled<hr/> 
 

 
<textarea disabled ></textarea>

相關問題