我正在尝试从文本框中获取文本。
我有两个不在表单中的输入文本框,我正在尝试检索value
并将其存储在一个变量中。
此代码在弹出的警告框中返回undefined
。
<script>
var userPass = document.getElementById('pass');
var userName = document.getElementById('fName');
function submit(){
alert(userPass.value);
}
</script>
当我在警告函数中使用userName.value
作为参数运行它时,它将工作并显示您在框中键入的任何内容。
下面是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>
https://stackoverflow.com/questions/8874960
复制相似问题