最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - $(this) not working inside the success function of ajax - Stack Overflow

programmeradmin4浏览0评论

I am using ASP.NET MVC with Jquery, and this seems to be a jquery fault.

I am making an ajax call to my method, my code is

$('.reopenBtn').live('click', function () {
    var taskId = $(this).attr("data-taskid");

    $.ajax({
        url: '/Task/ReopenTask/?strTaskId=' + taskId,
        type: "POST",
        success: function (data) {
            // this does not work !!
            $(this).parent().parent().closest("div").remove();              
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Error');
        }
    });
});

The remove doesn't work however when creating a jsfiddle for this question here , this works.

So, is $(this) something different inside the success function of the ajax call ?

How do I get around this ? Thanks

I am using ASP.NET MVC with Jquery, and this seems to be a jquery fault.

I am making an ajax call to my method, my code is

$('.reopenBtn').live('click', function () {
    var taskId = $(this).attr("data-taskid");

    $.ajax({
        url: '/Task/ReopenTask/?strTaskId=' + taskId,
        type: "POST",
        success: function (data) {
            // this does not work !!
            $(this).parent().parent().closest("div").remove();              
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Error');
        }
    });
});

The remove doesn't work however when creating a jsfiddle for this question here , this works.

So, is $(this) something different inside the success function of the ajax call ?

How do I get around this ? Thanks

Share edited Sep 23, 2012 at 6:10 Lorenz Lo Sauer 24.7k16 gold badges87 silver badges87 bronze badges asked Sep 23, 2012 at 5:56 Yasser ShaikhYasser Shaikh 47.8k50 gold badges208 silver badges287 bronze badges 1
  • possible duplicate of $(this) inside of AJAX success not working – Felix Kling Commented May 28, 2013 at 15:14
Add a ment  | 

3 Answers 3

Reset to default 8

context property will work inside the success function of ajax context: this,

$('.reopenBtn').live('click', function () {
    var taskId = $(this).attr("data-taskid");
    var self = this;
    $.ajax({
        url: '/Task/ReopenTask/?strTaskId=' + taskId,
        type: "POST",
        success: function (data) {
            $(self).parent().parent().closest("div").remove();              
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert('Error');
        }
    });
});

Or you could set context property of ajax option.

    $('.reopenBtn').live('click', function () {
        var taskId = $(this).attr("data-taskid");
        $.ajax({
            url: '/Task/ReopenTask/?strTaskId=' + taskId,
            type: "POST",
            context: this,
            success: function (data) {
                $(this).parent().parent().closest("div").remove();              
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert('Error');
            }
        });
    });

I prefer this method, wrapping with $.proxy, two args are callback function and this as 2nd argument.

$.post('/foo', data, $.proxy(function(d){
  // do stuff with data
}, this));

I think it is shortest and cleanest.

You can do what others say above as well, either copying this to that (self is a reserved word) and then using in callback, or using $.ajax with context: this parameter.

The context of the click method is simply not the same as for the success method. Thus you do not have the same this in the inner function. You can avoid that by using a local variable like $this or clickedButton and use it in your success method:

$('.reopenBtn').live('click', function () {
  var $this = $(this)
  var taskId = $this.attr("data-taskid");

  $.ajax({
    url: '/Task/ReopenTask/?strTaskId=' + taskId,
    type: "POST",
    success: function (data) {
        // $this is taken from the outer function context
        $this.parent().parent().closest("div").remove();              
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('Error');
    }
  });
});

For detailed explanation you might want to take a look at Closures in JavaScript

发布评论

评论列表(0)

  1. 暂无评论