简体   繁体   English

无法设置disabled = false(javascript)

[英]Can't set disabled=false (javascript)

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(ID为DIV)和动作(false或true)并获取div中的所有输入和选择,并将其disabled属性设置为false或true。

According to Firebug, the elements inside the Div are disabled all the time. 根据Firebug,Div内部的元素一直处于禁用状态。 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. 我可以通过使用警告框看到这个函数被调用,实际上它确实设置了disabled = false。 But the elements are still disabled. 但元素仍然被禁用。

Something to point out is that according to firebug, the disabled attribute looks like this: 需要指出的是,根据firebug,disabled属性如下所示:

    <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 谢谢

Edited to reflect the comments. 编辑以反映评论。

According to the W3C the code you posted should be correct. 根据W3C,您发布的代码应该是正确的。 The disabled attribute is a boolean attribute. disabled属性是布尔属性。 Use of the removeAttribute() method may be helpful as well. 使用removeAttribute()方法也可能有所帮助。

In my experience, you can also achieve this effect using the string values 'disabled' or ''. 根据我的经验,您也可以使用字符串值'disabled'或''来实现此效果。 This may work because these values are coerced into a boolean representation when the browser encounters them. 这可能有效,因为当浏览器遇到这些值时,这些值被强制转换为布尔表示。

To disable elements you need to use attribute disabled = "disabled" rather than true or false . 要禁用元素,您需要使用属性disabled = "disabled"而不是truefalse To make it enabled again, you need to remove the disabled attribute. 要再次启用它,您需要删除disabled属性。 Modify your code like this: 像这样修改你的代码:

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 setAttribute and removeAttribute functions are used to set and remove disabled attribute respectively. setAttributeremoveAttribute函数分别用于设置和删除disabled属性。

try .disabled = null or .removeAttribute('disabled') . 尝试.disabled = null.removeAttribute('disabled') My understanding is that it's the presence or absence of the disabled attribute that governs disabledness, not its value. 我的理解是, disabled属性的存在与否决定了残疾,而不是其价值。

More code needed. 需要更多代码。 Everything looks correct, and setting the disabled property of an <input> element to a Boolean value (the correct approach) certainly works in Firefox, regardless of the presence or absence of the disabled attribute in the source HTML. 一切看起来都正确,并且将<input>元素的disabled属性设置为布尔值(正确的方法)当然可以在Firefox中使用,无论源HTML中是否存在disabled属性。

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

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