按下Enter时触发提交的Javascript

时间:2018-12-07 00:20:11

标签: javascript html css enter

我有一个密码表单,我想按Enter键来触发“提交”按钮,但似乎无法正常工作。我知道他们可以查看源代码来查看密码,这只是作为示例。

<!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>

3 个答案:

答案 0 :(得分:0)

只需将您的input // submit替换为button,然后使用form将函数移至onSubmit元素

<!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>

答案 1 :(得分:0)

您需要添加ID,操作,提交类型,防止默认设置和侦听器。签出我的代码段。

祝你有美好的一天!

<!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 :(得分:0)

只要放一个

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

在表单内。