登录监听Enter键
.html
<button class="submit" onclick="loginAction()" id="btnclick">登录</button>
.js
<script type="text/javascript ">
function getEncrypt(passwd) {
/* 公钥加密 */
let encrypt = new JSEncrypt();
let publicKey = '-----BEGIN PUBLIC KEY-----\n';
encrypt.setPublicKey(publicKey); //公钥的值
return encrypt.encrypt(passwd); //加密的字串值
}
let loginFun = function () {
let userName = getEncrypt($("#userName").val());
let userPasswd = getEncrypt($("#userPasswd").val());
//请求参数
var list = {
userName: userName,
userPasswd: userPasswd
};
$.ajax({
//请求方式
type: "POST",
//请求的媒体类型
contentType: "application/json;charset=UTF-8",
//请求地址
url: "/auth/login",
//数据,json字符串
data: JSON.stringify(list),
//请求成功
success: function (result) {
console.log(result);
// 登录成功之后转跳界面
window.location.href = '/url'
},
//请求失败,包含具体的错误信息
error: function (e) {
alert(JSON.parse(e.responseText).message)
}
});
}
// 登录
let loginAction = function () {
$("#btnclick ").click(
loginFun()
);
}
// 监听回车登录
$("body").keydown(function (event) {
var e = event || window.event || arguments.callee.caller.arguments[0];
if (e && e.keyCode == 13) {
$("#btnclick").click(loginFun());
}
});
</script>