我希望能够从表中删除一个用户,然后隐藏显示他们的div。
用户单击类.buddy_delete的按钮,其中包含要传递给删除它们的php文件的信息。
我有下面的工作正常,但我需要在AJAX响应中的$(this).parent().hide();,而不是在AJAX之前发生任何错误,这是可能的吗?
$('.buddy_delete').on('click', function(e) {
e.preventDefault();
var sei = $(this).attr('sei');
var sui = $(this).attr('sui');
$(this).parent().hide(); // ok here
$.ajax({
type: "POST",
url: '/ajax/actions/remove_user_from_list.php?sei=' + sei + '&sui=' + sui,
success: function (data) {
if(data==='ok'){
// $(this).parent().hide(); // doesn't work here, nothing happens
} else {
$('#errorBoxEvent').html('Houston, we have a problem!');
}
}
}); // End .ajax function
});发布于 2014-01-04 20:58:30
设置ajax方法的选项上下文:
$.ajax({
context: this,
//...
});发布于 2014-01-04 20:59:37
为什么不使用闭包来捕获$(this):
$('.buddy_delete').on('click', function(e) {
e.preventDefault();
var sei = $(this).attr('sei');
var sui = $(this).attr('sui');
var $that = $(this); //capture this
$(this).parent().hide(); // ok here
$.ajax({
type: "POST",
url: '/ajax/actions/remove_user_from_list.php?sei=' + sei + '&sui=' + sui,
success: function (data) {
if(data==='ok'){
$that.parent().hide(); //use here
} else {
$('#errorBoxEvent').html('Houston, we have a problem!');
}
}
}); // End .ajax function
});https://stackoverflow.com/questions/20926613
复制相似问题