繁体   English   中英

在javascript中按回车键登录?

[英]Login when press enter in javascript?

<input type="text" class="form-control" id="username" name="username" placeholder="Username" required="" autofocus="" />
<input type="password" class="form-control" id="password" name="pwd" placeholder="Password" required="" autofocus="" />
<input class="btn btn-lg btn-primary btn-block button marginT20"  name="login" value="Login" type="Submit" onclick="login(document.getElementById('username').value,document.getElementById('password').value)" >   

<script>  
functionlogin(username, password){
  $.ajax({
    method: "POST",
    url: "/login",
    data: {
      username: username,
      pwd: password
    }
  }).done(function(msg1){
    if(msg1=='success'){
      window.location='/home';
    }else{
      varusername='username';varpassword='password';eraseCookie(username,
      password);toastr.warning(msg1);
    }
  });
}  
</script>

当有人按下登录按钮时,我的代码正在工作,但如果用户按下回车键,我希望填写用户名和密码。 然后使用现有的ajax代码提交表单。

执行以下步骤:

  1. 将 HTML 代码包裹在表单周围
  2. 在您的表单中使用onsubmit属性调用与单击提交按钮相同的方法

试试这个

 $('#username , #password').keypress(function (e) {
  var key = e.which;
  if(key == 13)  // the enter key code
   {
        var username =$('#username').val();
        var password =$('#password').val()
        login(username, password);
   }
});

暂无
暂无

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

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