如何从 javascript 访问 HTML 文本框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4213547/
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 to access an HTML textbox from javascript?
提问by Ahmad Farid
How to access an HTML textbox by a javascript function?
如何通过 javascript 函数访问 HTML 文本框?
回答by Rich
Set ID property on text box and use document.getElementById() function... Example below:
在文本框上设置 ID 属性并使用 document.getElementById() 函数...示例如下:
<html>
<head>
<script type="text/javascript">
function doSomethingWithTextBox()
{
var textBox = document.getElementById('TEXTBOX_ID');
// do something with it ...
}
</script>
</head>
<body>
<input type="text" id="TEXTBOX_ID">
</body>
</html>
回答by Jonathon Oates
Very simply, try this:
很简单,试试这个:
<!doctype html>
<html>
<head>
…
</head>
<body>
<form>
<input id="textbox" type="text" />
</form>
<script>
var textboxValue = document.getElementById("textbox").value;
</script>
</body>
The variable textboxValue
would equal whatever you've typed into the textbox.
该变量textboxValue
将等于您在文本框中键入的任何内容。
Remember you must place your script, if written as simply as this, after the textbox (input
field) appears in your HTML, otherwise when the page first loads you'd get an error, because the script is looking for input
field that has not yet been created by the browser.
请记住,如果像这样简单地编写脚本,则必须将脚本input
放在 HTML 中出现的文本框(字段)之后,否则当页面首次加载时,您会收到错误消息,因为脚本正在寻找input
尚未找到的字段由浏览器创建。
I hope this helps!
我希望这有帮助!
回答by jolt
Give your textbox an id
attribute, and after, fetch it with document.getElementById('<textbox id>')
.
给你的文本框一个id
属性,然后用document.getElementById('<textbox id>')
.
回答by Ivo Wetzel
First you need to be able to get a DOM(Document Object Model) reference to the textbox:
首先,您需要能够获得对文本框的 DOM(文档对象模型)引用:
<input type="text" id="mytextbox" value="Hello World!" />
Notice the id
attribute, the textbox now has the id mytextbox
.
注意id
属性,文本框现在有 id mytextbox
。
Next step is to get the reference in JavaScript:
下一步是在 JavaScript 中获取引用:
var textbox = document.getElementById('mytextbox'); // assign the DOM element reference to the variable "textbox"
This will retrieve a HTML Element by its id
attribute. Note that those id's need to be unique, so you can't have two textboxes with the same id.
这将通过其id
属性检索 HTML 元素。请注意,这些 id 必须是唯一的,因此不能有两个具有相同 id 的文本框。
Now the final step is to retrieve the value of the textbox:
现在最后一步是检索文本框的值:
alert(textbox.value); // alert the contents of the textbox to the user
The value
property contains the contents of the textbox, and that's it!
该value
属性包含文本框的内容,仅此而已!
For more reference you might want to check out some stuff over at MDC:
GetElementByID Reference
Input Element Reference
A general overview of the DOM
如需更多参考,您可能需要查看 MDC 上的一些内容:
GetElementByID 参考
输入元素参考
DOM 的一般概述
回答by Yishai Landau
document.getElementById('textboxid').value or document.formname.textboxname.value
document.getElementById('textboxid').value 或 document.formname.textboxname.value