如何使用 JavaScript 禁用 TextBox?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5624988/
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
How do I disable TextBox using JavaScript?
提问by Mark
earlier I asked for help disabling a Button when a drop down menu item was selected. I was given some code that did the trick but now I need the same with a Textbox and for some odd reason its not working can you have a look for me...
早些时候我在选择下拉菜单项时请求帮助禁用按钮。我得到了一些可以解决问题的代码,但是现在我需要使用文本框进行相同的操作,并且由于某些奇怪的原因它无法正常工作,您可以看看我吗...
HTML:
HTML:
<form id="frmColor">
<input type='TextBox' id='color' />
<input type='submit' value='Change Color' id="colorSubmit"/>
</form>
Javascript:
Javascript:
tools.eraser = function () {
var tool = this;
this.started = false;
var varPenColor = "White";
context.strokeStyle = varPenColor;
document.getElementById('frmColor').colorSubmit.disabled=true;
document.getElementById('frmColor').color.disabled=true;
Any ideas why it wont disable the text box?
任何想法为什么它不会禁用文本框?
Thanks
谢谢
回答by Pointy
Form elements can be accessed via the form's DOM element by name, not by "id" value. Give your form elements names if you want to access them like that, or else access them directly by "id" value:
表单元素可以通过表单的 DOM 元素通过name 访问,而不是通过“id”值。如果你想像这样访问它们,给你的表单元素命名,或者直接通过“id”值访问它们:
document.getElementById("color").disabled = true;
edit— oh also, as pointed out by others, it's just "text", not "TextBox", for the "type" attribute.
编辑- 哦,还有,正如其他人所指出的,对于“type”属性,它只是“text”,而不是“TextBox”。
You might want to invest a little time in reading some front-end development tutorials.
您可能想花一点时间阅读一些前端开发教程。
回答by vijay as vj
With the help of jquery it can be done as follows.
在 jquery 的帮助下,它可以完成如下。
$("#color").prop('disabled', true);
回答by Kickass
Here was my solution:
这是我的解决方案:
Markup:
标记:
<div id="name" disabled="disabled">
Javascript:
Javascript:
document.getElementById("name").disabled = true;
This the best solution for my applications - hope this helps!
这是我的应用程序的最佳解决方案 - 希望这会有所帮助!
回答by Abhijit
You can use disabled attribute to disable the textbox.
您可以使用 disabled 属性来禁用文本框。
document.getElementById('color').disabled = true;