繁体   English   中英

捕获输入字段中的文本

[英]Capturing the text within an input field

我在弄清楚这段代码中我在做什么错时遇到了麻烦。 我只是试图为用户名和密码设置用户输入,并将它们存储在各自的变量中。 由于某种原因,变量始终为空字符串。 有人可以告诉我我要去哪里了吗?

HTML

<body>
<main>
    <h1>Login Form</h1>
    <form>
        <label for="username">Username:</label>
        <input type="text" name="username" id="username">
        <label for="password">Password:</label>
        <input type="text" name="password" id="password">
        <button id="submitButton" type="button">Submit</button>
    </form>
</main>
<script src="loginPage.js"></script>
</body>

JS

var submitButton = document.getElementById("submitButton");
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var user = {username, password};
console.log(user);

请记住,仅在更改输入后才填充变量。

 document.querySelector('input[name:username').addEventListener('change',(e)=>{ var username = e.target.value; } document.querySelector('input[name:password').addEventListener('change',(e)=>{ var password = e.target.value; } 
 <form> <label for="username">Username:</label> <input type="text" name="username" id="username"> <label for="password">Password:</label> <input type="text" name="password" id="password"> </form> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM