Javascript 如何禁用javascript中的控件

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

how to disable a control in javascript

javascript

提问by ppp

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

this works in IE but does not works in mozila. What shoul I do?

这在 IE 中有效,但在 mozila 中无效。我该怎么办?

回答by Mic

Did you try:

你试过了吗:

document.getElementById("ctrl").setAttribute('disabled', true);

回答by Massaoui

<body>
    <input id="btnSubmit" type="button" value="submit" onclick="disabled(this);"/>
    <script>
        function disabled(ctrl) {
            ctrl.disabled = true;
        }
    </script>
</body>

回答by Eric

It is hard to tell what the issue is that you are having. Does mozillado anything when the code is executed? does it display an error? What version of iedid you test it with? And can you also provide the htmlfor the ctrlelement?

很难说你遇到了什么问题。不Mozilla的代码执行时做什么?它是否显示错误?你用什么版本的ie测试的?您还可以提供该元素的htmlctrl吗?

One of the issue with IE and the getElementByIdmethod is that in some versions of the browser it will match on the idattribute of a tag as well as the nameattribute (which does not follow the JavaScript spec). In Mozilla it is only matching using the idattribute.

IE 和该getElementById方法的问题之一是,在某些版本的浏览器中,它将匹配id标签的name属性以及属性(不遵循JavaScript 规范)。在 Mozilla 中,它仅使用id属性进行匹配。