Javascript选择器错误

Javascript selector error

本文关键字:错误 选择器 Javascript      更新时间:2023-09-26

经过多次尝试(特别是这个问题),我仍然无法获得用户输入的文本:

<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link type="text/css" rel="stylesheet" href="css1.css">
        <link type="text/css" rel="stylesheet" href="login.css">
        <script src="js/jquery-2.1.1.min.js"></script>
    </head>
    <body>
        <div id="firstForm">
            <h2>Login</h2>
            <form id="signin" action="index.html" method='post'>
                <input id='login1' class="inputs" name="mail" type="email" placeholder="mail" required>
                <input id='password1' class="inputs" name="password" type="password" placeholder="password" required><br>
                <input class="inputs" type="submit" id="submitLogin">
            </form>
        </div>
</body>
    <script src='js/loginscript.js'></script>
</html>   
JS:
(function (){
        var login=document.getElementById("login1").value;
        var pwd=document.getElementById("password1").value;
        console.log(login + '   *   '+ pwd);
        document.getElementById('signin').addEventListener('submit',function(e){
            console.log(login + '   *   '+ pwd);
            e.preventDefault();
        },false);
    })();

两个控制台日志都打印出带有空白的*:nothing was caught…

谢谢!

在用户与其交互之前,您已经评估了loginpwd

请参考小提琴。更新JavaScript:

(function (){
    var login = document.getElementById("login1"), 
        pwd = document.getElementById("password1");
    document.getElementById('signin').addEventListener('submit',function(e){
        e.preventDefault();
        console.log(login.value + '   *   '+ pwd.value);
    },false);
})();