小编Bea*_*116的帖子

从文本框中获取输入值

我正试图从文本框中获取文本.

我有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();"/>&nbsp&nbsp&nbsp
            <input type="button" class="loginButtons" value="Cancel"/></td>
    </table>
Run Code Online (Sandbox Code Playgroud)

html javascript textbox input

19
推荐指数
3
解决办法
19万
查看次数

标签 统计

html ×1

input ×1

javascript ×1

textbox ×1