Javascript document.getElementById('btnid').disabled 在 Firefox 和 chrome 中不起作用

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

document.getElementById('btnid').disabled is not working in firefox and chrome

javascriptgetelementbyid

提问by steeve

I'm using JavaScript for disabling a button. Works fine in IE but not in FireFox and chrome, here is the script what I'm working on:

我正在使用 JavaScript 来禁用按钮。在 IE 中可以正常工作,但在 FireFox 和 chrome 中不能正常工作,这是我正在处理的脚本:

function disbtn(e) { 
    if ( someCondition == true ) {
       document.getElementById('btn1').disabled = true;
    } else {
       document.getElementById('btn1').disabled = false;
    }

And in my html I have:

在我的 html 中,我有:

<input type="button" id="btn1" value="submit" />

回答by Ahsan Khurshid

use setAttribute()and removeAttribute()

使用setAttribute()removeAttribute()

function disbtn(e) { 
    if ( someCondition == true ) {
       document.getElementById('btn1').setAttribute("disabled","disabled");
    } else {
       document.getElementById('btn1').removeAttribute("disabled");
    }
}

SEE DEMO

看演示

回答by laurent

Try setting the disabledattribute directly:

尝试disabled直接设置属性:

if ( someCondition == true ) {
   document.getElementById('btn1').setAttribute('disabled', 'disabled');
} else {
   document.getElementById('btn1').removeAttribute('disabled');
}

回答by Paul Aldred-Bann

There are always weird issues with browser support of getElementById, try using the following instead:

浏览器对 getElementById 的支持总是有一些奇怪的问题,请尝试使用以下内容:

// document.getElementsBySelector are part of the prototype.js library available at http://api.prototypejs.org/dom/Element/prototype/getElementsBySelector/

function disbtn(e) { 
    if ( someCondition == true ) {
        document.getElementsBySelector("#btn1")[0].setAttribute("disabled", "disabled");
    } else {
        document.getElementsBySelector("#btn1")[0].removeAttribute("disabled");
    }    
}

Alternatively, embrace jQuery where you could simply do this:

或者,拥抱 jQuery,你可以简单地这样做:

function disbtn(e) { 
    if ( someCondition == true ) {
        $("#btn1").attr("disabled", "disabled");
    } else {
        $("#btn1").removeAttr("disabled");
    }    
}

回答by Anand

Some time some javascript functions does not work on some specific browser. I would suggest you to start using JQuery, which gives you normalized JS, taking care of different browser requirement

有时某些 javascript 功能在某些特定浏览器上不起作用。我建议你开始使用 JQuery,它给你规范化的 JS,照顾不同的浏览器需求

$('#btn1').each(function(){
    this.disabled = false;
});

回答by HiCode

Another alternative :

另一种选择:

document.formname.elementname.disabled=true

Work on FF and IE ! :)

在 FF 和 IE 上工作!:)

回答by Vimal

I've tried all the possibilities. Nothing worked for me except the following. var element = document.querySelectorAll("input[id=btn1]"); element[0].setAttribute("disabled",true);

我已经尝试了所有的可能性。除了以下内容外,没有什么对我有用。var element = document.querySelectorAll("input[id=btn1]"); element[0].setAttribute("disabled",true);

回答by native

stay true to native (Boolean) property support and its powerful syntax like:

忠实于原生(布尔)属性支持及其强大的语法,例如:

[elem].disabled = condition ? true : false;//done!

[elem].disabled = 条件?真假; //完毕!

and for our own good collective coding experience, -please insist on others to support it as well.

为了我们自己良好的集体编码体验,请坚持其他人也支持它。