无法设置 disabled=false (javascript)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3557266/
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-23 05:10:15  来源:igfitidea点击:

Can't set disabled=false (javascript)

javascripthtmlforms

提问by g.d.d.c

I have this function:

我有这个功能:

function disableDiv(divId, action){
    var divId = byId(divId);

    if(action==true){
    divId.style.display='none';
    }
    else if(action==false){
    divId.style.display='block';
    }

    var inputs = divId.getElementsByTagName("input");
    var selects = divId.getElementsByTagName("select");
    var i;

    for (i=0; i<inputs.length; i++){
        inputs[i].disabled=action;
        }

    for (i=0; i<selects.length; i++){
        selects[i].disabled=action;
        }
}

This takes a divId (id of DIV) and an action (false or true) and gets all inputs and selects inside the div, and sets their disabled attribute to either false or true.

这需要一个 divId(DIV 的 id)和一个动作(false 或 true)并获取 div 内的所有输入和选择,并将它们的 disabled 属性设置为 false 或 true。

According to Firebug, the elements inside the Div are disabled all the time. But they should be active once hitting a drop-list option... The triggering is fine so you know. I can see this function beeing called by using alert boxes, and it does in fact set the disabled=false. But the elements are still disabled.

根据 Firebug 的说法,Div 内的元素一直处于禁用状态。但是一旦点击下拉列表选项,它们就应该处于活动状态......触发很好,所以你知道。我可以看到使用警告框调用了这个函数,它实际上设置了 disabled=false。但是元素仍然被禁用。

Something to point out is that according to firebug, the disabled attribute looks like this:

需要指出的是,根据firebug,禁用属性如下所示:

    <input name="test" id="test" disabled="">

Note there is just two doublequotes... Shouldn't it say "disabled='disabled'" or "disabled=true"?

请注意只有两个双引号......它不应该说“disabled='disabled'”或“disabled=true”吗?

Any tips on how to troubleshoot further?

有关如何进一步排除故障的任何提示?

Here is how I call the function:

这是我调用函数的方式:

(category=="Cars")?disableDiv("nav_sub_cars", false):disableDiv("nav_sub_cars", true);

If you need more input, just let me know...

如果您需要更多输入,请告诉我...

Thanks

谢谢

回答by g.d.d.c

Edited to reflect the comments.

编辑以反映评论。

According to the W3C the code you posted should be correct. The disabled attribute is a boolean attribute. Use of the removeAttribute()method may be helpful as well.

根据 W3C,您发布的代码应该是正确的。disabled 属性是一个布尔属性。使用该removeAttribute()方法也可能有所帮助。

In my experience, you can also achieve this effect using the string values 'disabled' or ''. This may work because these values are coerced into a boolean representation when the browser encounters them.

根据我的经验,您也可以使用字符串值 'disabled' 或 '' 来实现这种效果。这可能有效,因为当浏览器遇到这些值时,这些值会被强制转换为布尔值表示。

回答by Sarfraz

To disable elements you need to use attribute disabled = "disabled"rather than trueor false. To make it enabled again, you need to remove the disabledattribute. Modify your code like this:

要禁用元素,您需要使用属性disabled = "disabled"而不是truefalse。要再次启用它,您需要删除该disabled属性。像这样修改你的代码:

for (i=0; i<inputs.length; i++){
  if (action === false) {
    inputs[i].removeAttribute('disabled');
  }
  else {
    inputs[i].setAttribute('disabled', 'disabled');
  }
}

for (i=0; i<selects.length; i++){
  if (action === false) {
    selects[i].removeAttribute('disabled');
  }
  else {
    selects[i].setAttribute('disabled', 'disabled');
  }
}

The setAttributeand removeAttributefunctions are used to set and remove disabledattribute respectively.

setAttributeremoveAttribute功能用于设置和删除disabled分别属性。

回答by Stuart

try .disabled = nullor .removeAttribute('disabled'). My understanding is that it's the presence or absence of the disabledattribute that governs disabledness, not its value.

尝试.disabled = null.removeAttribute('disabled')。我的理解是,disabled控制残疾的是属性的存在与否,而不是它的价值。

回答by Tim Down

More code needed. Everything looks correct, and setting the disabledproperty of an <input>element to a Boolean value (the correct approach) certainly works in Firefox, regardless of the presence or absence of the disabledattribute in the source HTML.

需要更多代码。一切看起来都正确,并且将元素的disabled属性设置<input>为布尔值(正确的方法)在 Firefox 中肯定有效,无论disabled源 HTML 中是否存在该属性。