我执行ajax后怎么不能 $(this).removeClass("disabled");
这个 $(this).removeClass("disabled");
应该加在什么地方
$("#page").delegate("#login_frame .login .mail-login .btn","click",function(){
if ($(this).hasClass("disabled")) return;
var email = $(".mail-login input[name = 'email']").val().trim(),
password = $(".mail-login input[name = 'password']").val().trim(),
isEmail = function() {
if(email == "") {
alert("请输入您的邮箱地址");
return;
} else if(!checkEmail(email)) {
alert("请输入正确的邮箱地址");
return;
} else {
return true;
}
},
isPassword = function() {
if(password == "") {
alert("请输入密码");
return;
} else {
return true;
}
};
if(isEmail() && isPassword()) {
$(this).addClass("disabled");
$.ajax({
type: "POST",
url: "/auth/",
data: {email:email, password:password},
dataType: "json",
success: function(data){
if(data.err == 403) {
console.log(data.err);
alert(data.msg);
} else if(data.err == 200) {
window.location.reload();
}
$(this).removeClass("disabled");
}
});
}
});
ringa_lee2017-04-10 14:48:46
http://api.jquery.com/jquery.ajax/
jQuery的ajax选项里面有一个context参数,就是定义回调的上下文的。
$.ajax({
url: "test.html",
context: this,
success: function(){ //这里面的this上下文就是context的值了。 }
});
阿神2017-04-10 14:48:46
有可能是this
指向的问题,在addClass
的时候增加一个定义
var that = $(this);
然后在回调中使用that
而不是$(this)
。