我正试图从文本框中获取文本.
我有2个不在表单中的输入文本框,我正在尝试检索value并将其存储在变量中.
此代码undefined在弹出的警告框中返回.
<script>
var userPass = document.getElementById('pass');
var userName = document.getElementById('fName');
function submit(){
alert(userPass.value);
}
</script>
Run Code Online (Sandbox Code Playgroud)
当我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>
Run Code Online (Sandbox Code Playgroud)