我正在尝试获取文本框中的文本。
我有两个输入文本框,它们不在一个表单中,我正在尝试检索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>