首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ajax响应中的JQuery父()隐藏()

ajax响应中的JQuery父()隐藏()
EN

Stack Overflow用户
提问于 2014-01-04 20:51:42
回答 2查看 143关注 0票数 0

我希望能够从表中删除一个用户,然后隐藏显示他们的div。

用户单击类.buddy_delete的按钮,其中包含要传递给删除它们的php文件的信息。

我有下面的工作正常,但我需要在AJAX响应中的$(this).parent().hide();,而不是在AJAX之前发生任何错误,这是可能的吗?

代码语言:javascript
复制
$('.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
    });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-04 20:58:30

设置ajax方法的选项上下文:

代码语言:javascript
复制
$.ajax({
   context: this,
   //...
});
票数 1
EN

Stack Overflow用户

发布于 2014-01-04 20:59:37

为什么不使用闭包来捕获$(this):

代码语言:javascript
复制
$('.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
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20926613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档