Javascript 从文本框中获取输入值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8874960/
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
Getting input values from text box
提问by Beanno1116
I'm trying to get the text from a text box.
我正在尝试从文本框中获取文本。
I have 2 input text boxes that are not in a form, and I'm trying to retrieve the value
and store it in a variable.
我有 2 个不在表单中的输入文本框,我正在尝试检索它value
并将其存储在一个变量中。
This code returns undefined
in the alert box that pops up.
此代码undefined
在弹出的警告框中返回。
<script>
var userPass = document.getElementById('pass');
var userName = document.getElementById('fName');
function submit(){
alert(userPass.value);
}
</script>
When I run it with userName.value
as a parameter in the alert function, it will work and display what ever you type in the box.
当我将它userName.value
作为警报函数中的参数运行时,它将起作用并显示您在框中键入的任何内容。
Here is the html:
这是html:
<table id="login">
<tr>
<td><label>User Name</label></td>
</tr>
<tr>
<td colspan="2"><input class="textBox" id="fName" type="text" maxlength="30" required/></td>
</tr>
<tr>
<td id="pass"><label>Password</label></td>
<tr>
<td colspan="2"><input class="textBox" id="pass" type="text" maxlength="30" required/></td>
</tr>
<tr>
<td><input type="button" class="loginButtons" value="Login" onclick="submit();"/>   
<input type="button" class="loginButtons" value="Cancel"/></td>
</table>
采纳答案by Itay Moav -Malimovka
You will notice you have no value attr in the input
tags.
Also, although not shown, make sure the Javascript is run afterthe html is in place.
您会注意到input
标签中没有值 attr 。
此外,虽然未显示,但请确保在 html 到位后运行 Javascript 。
回答by Emil Carr
<script>
function submit(){
var userPass = document.getElementById('pass');
var userName = document.getElementById('user');
alert(user.value);
alert(pass.value);
}
</script>
<input type="text" id="user" />
<input type="text" id="pass" />
<button onclick="submit();" href="javascript:;">Submit</button>
回答by Rex CoolCode Charles
// NOTE: Using "this.pass" and "this.name" will create a global variable even though it is inside the function, so be weary of your naming convention
// 注意:使用 "this.pass" 和 "this.name" 将创建一个全局变量,即使它在函数内部,所以要厌倦你的命名约定
function submit()
{
var userPass = document.getElementById("pass").value;
var userName = document.getElementById("user").value;
this.pass = userPass;
this.name = userName;
alert("whatever you want to display");
}
回答by hvgotcodes
you have multiple elements with the same id. That is a big no-no. Make sure your inputs have uniqueids.
您有多个具有相同 ID 的元素。这是一个很大的禁忌。确保您的输入具有唯一的ID。
<td id="pass"><label>Password</label></td>
<tr>
<td colspan="2"><input class="textBox" id="pass" type="text" maxlength="30" required/></td>
</tr>
see, both the td
and the input
share the id value pass
.
看, thetd
和 theinput
共享 id value pass
。
回答by aziz punjani
Remove the id="pass"
off the td
element. Right now the js will get the td element instead of the input hence the value is undefined.
删除id="pass"
关闭td
元素。现在 js 将获得 td 元素而不是输入,因此该值未定义。
回答by Sham
Javascript document.getElementById("<%=contrilid.ClientID%>").value; or using jquery
Javascript document.getElementById("<%=contrilid.ClientID%>").value; 或使用 jquery
$("#<%= txt_iplength.ClientID %>").val();
$("#<%= txt_iplength.ClientID %>").val();
回答by nid
This is the sample code for the email and javascript.
这是电子邮件和 javascript 的示例代码。
params = getParams();
subject = "ULM Query of: ";
subject += unescape(params["FormsEditField3"]);
content = "Email: ";
content += unescape(params["FormsMultiLine2"]);
content += " Query: ";
content += unescape(params["FormsMultiLine4"]);
var email = "[email protected]";
document.write('<a href="mailto:'+email+'?subject='+subject+'&body='+content+'">SUBMIT QUERY</a>');