Ajax验证用户名体验
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
function nameFun() {
$.post({
url: "${pageContext.request.contextPath}/a3",
data: {"name": $("#name").val()},
success: function (data) {
//console.log(data);
if (data.toString() === "用户名成功") {//后端返回对应的信息进行比对
$("#userInfo").css("color", "green");
} else {
$("#userInfo").css("color", "red");
}
$("#userInfo").html(data);
}
})
}
function passwordFun() {
$.post({
url: "${pageContext.request.contextPath}/a3",
data: {"password": $("#password").val()},
success: function (data) {
if (data.toString() === "密码正确") {
$("#userPasswordInfo").css("color", "green");
}else {
$("#userPasswordInfo").css("color", "red");
}
$("#userPasswordInfo").html(data);
}
})
}
</script>
</head>
<body>
<p>
用户名:<input type="text" id="name" onclick="nameFun()">
<%--span提示信息--%>
<span id="userInfo"></span>
</p>
<p>
用户密码:<input type="password" id="password" onclick="passwordFun()">
<span id="userPasswordInfo"></span>
</p>
</body>
</html>
内容参考:https://www.bilibili.com/video/BV1aE41167Tu
仅用于学习!