html片段
function IbtnEnter_onclick() {
checklogin();
return false;
}
function checklogin() {
if ($("#username").val() == "" || $("#username").val().length < 5 || $("#username").val().length > 30 ) {
$("#tip").html("提示:用户名必须填写,并且长度必须在5-30个字符之间!");
$("#username").focus();
return false;
}
else{
$("#tip").html(" ");
}
if ($("#password").val() == "" || $("#password").val().length < 8 || $("#password").val().length > 20 ) {
$("#tip").html("提示:密码必须填写,并且长度必须在8-20个字符之间!");
$("#password").focus();
return false;
}
else{
$("#tip").html(" ");
}
if ($("#verifycode").val().trim() == "" || $("#verifycode").val().trim() != $("#hiddencode").val().trim()) {
$("#tip").html("提示:验证码计算有误!");
$("#verifycode").focus();
return false;
}
else{
$("#tip").html(" ");
}
$.ajax({
type: "POST",
url: "userLogin.do",
dataType : 'text',
data : $('#loginform').serialize(),
success: function (data) {
if (data == 1) {
location.href = "index.jsp";
return true;
}
else {
alert("登入失败,用户名或密码错误!");
$("#password").val("");
$("#verifycode").val("");
getverifycode();
$("#password").focus();
return false;
}
}
})
}
</script>
<input name="button1" type="button" class="button" id="button1" value=" 登 陆 " οnclick="IbtnEnter_onclick();">
tip:
如果成功返回1
PrintWriter out = null;
try {
out = response.getWriter();
out.println("1");
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
扩展:
ajax另一种方式:返回json字符串
示例代码:
function del(id){
$.ajax( {
type : "POST",
url : "<%=request.getContextPath()%>/student.do?method=del&id=" + id,
dataType: "json",
success : function(data) {
if(data.del == "true"){
alert("删除成功!");
$("#" + id).remove();
}
else{
alert("删除失败!");
}
if(data.tip =="tipstr"){
alert("tipstr");
}
},
error :function(){
alert("网络连接出错!");
}
});
}
response.getWriter().print("{\"del\":\"true\",\"tip\":\"tipstr\"}");