简体   繁体   English

JavaScript选择器错误

[英]Javascript selector error

after several tries (especially this question ) I still can't get the text that the user enters in my inputs : 经过几次尝试(尤其是这个问题 ),我仍然无法获得用户在我的输入中输入的文本:

<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 : 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);
    })();

Both console logs print out the * with blanks : nothing was caught... 两个控制台日志都打印出带空格的*:什么也没发现...

Thank you! 谢谢!

You have evaluated the login and pwd even before the user has interacted with it. 您已经评估了loginpwd甚至在用户与之交互之前。

Please refer the fiddle . 请参阅小提琴 Updated JavaScript: 更新的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);
})();

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

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