我如何使用 javascript 设置文本框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4541855/
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 set value of a textbox using javascript
提问by Prady
I am trying to get a value fro query string and assign that value into a textbox. I am able to get the value from query string but unable to assign it to textbox.
我正在尝试从查询字符串中获取一个值并将该值分配给一个文本框。我能够从查询字符串中获取值,但无法将其分配给文本框。
document.getElementByName('Contact0Email').Value = email;
Tried the above code but doesn't seem to be working. Though the alert of email gives the right value.
尝试了上面的代码,但似乎不起作用。尽管电子邮件的警报提供了正确的价值。
回答by lonesomeday
You need a lower-case value
and a plural Elements
:
你需要一个小写value
和一个复数Elements
:
document.getElementsByName('Contact0Email')[0].value = email;
You need the [0]
to get the first element in the list. Names don't have to be unique like ids.
您需要[0]
获取列表中的第一个元素。名称不必像 id 一样唯一。