[英]jquery not working inside if after AJAX success/failure
我刚开始学习AJAX张贴我已经搜查,refered像以前问问题之前这个 ,但它并没有帮助以及我的代码是:
$('.del').click(function(){
$(this).parent().hide('slow');
$.get( "index.php", {del:$(this).attr('id')}).done(function(data){
if (data==='1') {
$(this).parent().remove();
}
else{
$(this).parent().show('slow');
}
});
});
内部的jquery语句if
执行以及要执行else
操作为了验证一切正常,我在语句中添加了一个alert
,该alert
成功弹出,但是jquery语句不起作用。这是什么问题以及如何解决此问题?
我相信您的$(this)
参考在该范围内不正确。 尝试:
$('.del').click(function(){
var $this = $(this);
$this.parent().hide('slow');
$.get( "index.php", {del:$this.attr('id')}).done(function(data){
if (data==='1') {
$this.parent().remove();
}
else{
$this.parent().show('slow');
}
});
});
我认为您的'this'变量可能超出了回调的范围。 尝试绑定它,或使用jQuery的proxy()
功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.