完成ajax调用后选择“this”元素

时间:2014-09-03 08:14:33

标签: jquery ajax

使用Jquery,在完成ajax调用之后,我怎么能说出你刚刚点击的这个元素remove()?使用以下代码不起作用:

$( ".deletegid" ).click(function() {   
    var imageid = $(this).attr('name');
    $.ajax({
        type: "POST",
        url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
        data: {
            imageid: imageid,
            galleryid: $("input[name=athletes_gid]").val(),
            ci_csrf_token: $("input[name=ci_csrf_token]").val()
        }
    })
    .done(function() {
        $(this).remove();           
    });
});

3 个答案:

答案 0 :(得分:4)

使用ajax的context选项将上下文设置为当前元素:

$.ajax({
    type: "POST",
    context:this,
    url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
    data: { imageid: imageid ,galleryid: $("input[name=athletes_gid]").val(), ci_csrf_token: $("input[name=ci_csrf_token]").val() }
})
.done(function() {
    $(this).remove();           
});

答案 1 :(得分:1)

将其保存在变量中

$(".deletegid").click(function() {
    var obj = $(this);
    var imageid = $(this).attr('name');
    $.ajax({
            type: "POST",
            url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
            data: {
                imageid: imageid,
                galleryid: $("input[name=athletes_gid]").val(),
                ci_csrf_token: $("input[name=ci_csrf_token]").val()
            }
        })
        .done(function() {
            obj.remove();
        });
});

this引用done事件中的ajax对象。

答案 2 :(得分:0)

用作

$(".deletegid").click(function() {
var objectControl = $(this);
var imageid = objectControl.attr('name');
$.ajax({
        type: "POST",
        url: "/public/index.php/admin/content/athletes/deleteimagefromgallery",
        data: {
            imageid: imageid,
            galleryid: $("input[name=athletes_gid]").val(),
            ci_csrf_token: $("input[name=ci_csrf_token]").val()
        }
    })
    .done(function() {
        objectControl.remove();
    });

});