【发布时间】:2012-02-11 02:18:24
【问题描述】:
我正在尝试从文本框中获取文本。
我有 2 个不在表单中的输入文本框,我正在尝试检索 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>
【问题讨论】:
-
-
我不同意马修的观点。继续做你正在做的事情,并学习基础知识。似乎很多 jQuery 用户都跳过了这一步。然后,当您有了坚实的基础后,您可能会也可能不会决定使用众多 JavaScript 库之一。
-
我发现它有和 id 名为 pass on 事故。谢谢大家
标签: javascript html input textbox