Javascript .setAttribute("禁用", false); 将可编辑属性更改为 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7526601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:36:13  来源:igfitidea点击:

.setAttribute("disabled", false); changes editable attribute to false

javascriptfirefoxfirefox-addonxul

提问by Ghokun

I want to have textboxes related to radiobuttons. Therefore each radio button should enable it's textbox and disable the others. However when I set the disabled attribute of textbox to true, it changes the editable attribute too. I tried setting editable attribute true again but it did not work.

我想要与单选按钮相关的文本框。因此,每个单选按钮都应该启用它的文本框并禁用其他的。但是,当我将文本框的禁用属性设置为 true 时,它​​也会更改可编辑属性。我再次尝试将可编辑属性设置为 true,但没有奏效。

This was what I tried:

这是我尝试过的:

JS function:

JS函数:

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

XUL elements:

XUL 元素:

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

回答by oezi

A disabledelement is, (self-explaining) disabled and thereby logically not editable, so:

disabled元件是,(自我解释)禁用,从而在逻辑上不可编辑的,这样:

set the disabled attribute [...] changes the editable attribute too

设置禁用属性 [...] 也会更改可编辑属性

Is an intended and well-defined behaviour.

是一种预期的和明确定义的行为。

The real problem here seems to be you're trying to set disabledto falsevia setAttribute()which doesn't do what you're expecting. an element is disabled if the disabled-attribute is set, independent of it's value (so, disabled="true", disabled="disabled"and disabled="false"all do the same: the element gets disabled). you should instead remove the complete attribute:

这里真正的问题似乎是您试图设置disabledfalsevia setAttribute(),但它没有达到您的预期。如果disabled设置了-attribute,则元素将被禁用,与它的值无关(因此,disabled="true"disabled="disabled"disabled="false"都做同样的事情:元素被禁用)。你应该删除完整的属性:

element.removeAttribute("disabled");

or set that property directly:

或直接设置该属性:

element.disabled = false;

回答by RobG

Just set the property directly: .

直接设置属性即可: .

eleman.disabled = false;

回答by antelove

Using method setand removeattribute

使用方法设置删除属性

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>

回答by Richard Dalton

Try doing this instead:

尝试这样做:

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

To enable an element you have to remove the disabled attribute. Setting it to false still means it is disabled.

要启用元素,您必须删除 disabled 属性。将它设置为 false 仍然意味着它被禁用。

http://jsfiddle.net/SRK2c/

http://jsfiddle.net/SRK2c/

回答by Baz1nga

the disabled attributes value is actally not considered.. usually if you have noticed the attribute is set as disabled="disabled" the "disabled" here is not necessary persay.. thus the best thing to do is to remove the attribute.

实际上不考虑禁用属性值..通常如果您注意到属性设置为禁用=“禁用”,则不必说这里的“禁用”..因此最好的办法是删除该属性。

element.removeAttribute("disabled");     

also you could do

你也可以做

element.disabled=false;

回答by Baz1nga

just replace 'myselect' with your id

只需将“myselect”替换为您的 ID

to disable->

禁用->

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

to enable->

启用->

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