Home > Article > Web Front-end > jquery clicks the enter key to achieve login effect example sharing
When I was doing project login recently, I had to click the login button every time to enter the corresponding page, which gave the user a very bad experience, so I added the use of the Enter key to log in. This article mainly shares with you a jquery method of clicking the Enter key to achieve the login effect and default focus. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.
Method:
<form> <input type="text" id="username_txt" placeholder="用户名" /> <input type="password" id="userpass_txt" placeholder="密码" /> <button id="login_btn">登录</button> </form> $(function () { $('#username_txt').focus(); //用户点击按钮 $("#login_btn").click(function () { //获取用户名 var username = $('#username_txt').val(); //获取密码 var userpass = $('#userpass_txt').val(); if (username == "" || userpass == "") { alert("用户名密码不能为空!"'); } else { //调用登录方法 $.ajax({ }); } }); $("body").keydown(function(event) { if (event.keyCode == "13") {//keyCode=13是回车键 $("#login_btn").click(); } }); });
Final rendering:
##Related recommendations:jquery Enter key trigger event implementation method
Analyze the differences between input submit, button and enter key to submit data
Detailed explanation of input submit, button and enter key to submit data
The above is the detailed content of jquery clicks the enter key to achieve login effect example sharing. For more information, please follow other related articles on the PHP Chinese website!