【问题标题】:Select "this" element after completed ajax call [duplicate]完成ajax调用后选择“this”元素[重复]
【发布时间】:2014-09-03 08:14:33
【问题描述】:

使用 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();           
    });
});

【问题讨论】:

    标签: jquery ajax


    【解决方案1】:

    使用 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();           
    });
    

    【讨论】:

      【解决方案2】:

      将其保存在变量中

      $(".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 对象。

      【讨论】:

        【解决方案3】:

        用作

        $(".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();
            });
        

        });

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-04
          • 1970-01-01
          • 2015-01-31
          • 1970-01-01
          • 2017-11-14
          • 2020-12-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多