$(function(){
$(document).keydown(function(event){
if(event.keyCode==13){
$("#login").click();
}
});
$("#login").click(function(){
$("#count_span").html("");
$("#pwd_span").html("");
//获取请求参数
var name = $("#count").val().trim();
var password = $("#password").val().trim();
var isCheck = true;
//检查格式
if(name==""){
$("#count_span").html("用户名为空");
isCheck = false;
}
if(password==""){
$("#pwd_span").html("密码为空");
isCheck = false;
}
//发送ajax请求
if(isCheck){
$.ajax({
url:url_path+"/user/login.do",
type:"post",
data:{"name":name,"password":password},
dataType:"json",
success:function(result){
//正常回调
if(result.status==0){
//获得用户ID,写入cookie
var userId = result.data;
addCookie("userId",userId,2);
window.location.href="edit.html";//成功
}else if(result.status==1){
$("#count_span").html(result.msg);
}else if(result.status==2){
$("#pwd_span").html(result.msg);
}
},
error:function(){
alert("登录失败");
}
});
}
});
});
转载于:https://my.oschina.net/u/2619068/blog/653873