【问题标题】:Javascript to trigger submit when enter is pressed按下输入时触发提交的Javascript
【发布时间】:2018-12-07 00:20:10
【问题描述】:

我有一个密码表单,我希望输入键触发提交按钮,但我似乎无法让它工作。我知道他们可以查看源代码以查看密码,这只是为了练习示例。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form>
        <input id="user" type="text" name="username" 
placeholder="username">
        <input id="pass" type="password" id="pass" name="password">
        <input id="subm"type="button" name="submit" value="submit" 
onclick="checkPswd();">
</form>    
</body>

<script type="text/javascript">  
       function checkPswd(){
        var confirmpassword = "admin";
        var username = document.getElementById('user').value;
        var password = document.getElementById("pass").value;

            if(password == confirmpassword){

                window.location.href = "redu.html";

            }else{
                alert('try again');
            }

       };

 </script>    
</html>

【问题讨论】:

标签: javascript html css enter


【解决方案1】:

你需要添加 id、action、submit type、prevent default 和 listener。看看我的 sn-p..

祝你有美好的一天!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
    <!-- add id and action -->
    <form id="example" action="#">
        <input id="user" type="text" name="username" placeholder="username">
        <input id="pass" type="password" id="pass" name="password">

        <!-- change type to submit -->
        <input id="subm" type="submit" name="submit" value="submit">
    </form>    
</body>

<script type="text/javascript"> 

//add listener
document.getElementById("example").addEventListener("submit", checkPswd, true);

function checkPswd(){
    var confirmpassword = "admin";
    var username = document.getElementById('user').value;
    var password = document.getElementById("pass").value;

    if(password == confirmpassword){

        window.location.href = "redu.html";

    }else{
        alert('try again');
    }

    //add prevent default
    event.preventDefault();
};

</script>    
</html>

【讨论】:

    【解决方案2】:

    只需将您的input // submit 替换为button 并使用onSubmit 将函数移动到form 元素

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    </head>
    <body>
    <form>
            <input id="user" type="text" name="username" 
    placeholder="username" onSubmit="checkPswd()">
            <input id="pass" type="password" id="pass" name="password">
            <button type="submit">submit</button>
    </form>    
    </body>
    
    <script type="text/javascript">  
          function checkPswd(){
            var confirmpassword = "admin";
            var username = document.getElementById('user').value;
            var password = document.getElementById("pass").value;
    
                if(password == confirmpassword){
    
                    window.location.href = "redu.html";
    
                }else{
                    alert('try again');
                }
    
          };
    
    </script>    
    </html>
    

    【讨论】:

      【解决方案3】:

      放一个

      <button type="submit">Submit</button>
      

      在您的表单中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-24
        相关资源
        最近更新 更多