代码:
html表单代码:
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>回车键实现登录方法</title> <script src="js/jquery-latest.min.js"></script> </head> <body>
<form id="loginForm"> <div class="form-group row"> <p>用户名</p> <input style="width: 100%;" type="text" class="form-control" id="login" placeholder="请输入帐号"> </div> <input type="button" class="btn btn-success" onclick="loginSubmit()" value="登陆"> </form>
</body>
</html>
要先引入JQuery文件(最新的jQuery版本更好)
Jq代码:
<javascript>
$("#loginForm").keydown(function (e){ var theEvent = window.event || e; var code = theEvent.keyCode || theEvent.which; if(code == 13){//回车键码是13 loginSubmit(); } })
function loginSubmit(){
alert("调用成功");
}
</javascript>