无法设置disabled = false(javascript) [英] Can't set disabled=false (javascript)

查看:110
本文介绍了无法设置disabled = false(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数:

  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 选择[i] .disabled = action;


$ / code $ / pre
$ b $ p这需要divId(DIV的ID)和一个action(false或true),并获取div内的所有输入和选择,并将其disabled属性设置为false或true。



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



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

 < input name =testid =testdisabled => 

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



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

该函数:

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

如果您需要更多输入信息,请让我知道...



谢谢

解决方案

编辑以反映评论。

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

根据我的经验,您也可以实现使用字符串值'disabled'或''的效果。这可能会起作用,因为这些值在浏览器遇到它们时被强制为布尔表示形式。


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.

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.

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

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

Note there is just two doublequotes... Shouldn't it say "disabled='disabled'" or "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. The disabled attribute is a boolean attribute. Use of the removeAttribute() method may be helpful as well.

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 = false(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆