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

javascript - jQuery: using $(this) inside of $.ajax success function - Stack Overflow

programmeradmin0浏览0评论

I'm making an $.ajax call, and the following code does not work as intended. The alert results in 'undefined'

$(document).ready( function {
  $(".elem").on("click", function(e) {

    e.preventDefault();

    $.ajax( {
      url: 'index.php',
      data: {
        'action': 'something'
      },
      success: function() {
        alert($(this).data("foobar"));
      }
    });
  });
)};

However, I was able to get it working by adding an alias to $(this) before entering the ajax function.

$(document).ready( function {
  $(".elem").on("click", function(e) {

    var old_this = $(this);

    e.preventDefault();

    $.ajax( {
      url: 'index.php',
      data: {
        'action': 'something'
      },
      success: function() {
        alert(old_this.data("foobar"));
      }
    });
  });
)};

I can't assign unique IDs to the element being clicked, so accessing it via $("#id") isn't an option.

Is there a more standardized approach to accessing the $(this) that existed before entering the success function or does this way work just fine?

I'm making an $.ajax call, and the following code does not work as intended. The alert results in 'undefined'

$(document).ready( function {
  $(".elem").on("click", function(e) {

    e.preventDefault();

    $.ajax( {
      url: 'index.php',
      data: {
        'action': 'something'
      },
      success: function() {
        alert($(this).data("foobar"));
      }
    });
  });
)};

However, I was able to get it working by adding an alias to $(this) before entering the ajax function.

$(document).ready( function {
  $(".elem").on("click", function(e) {

    var old_this = $(this);

    e.preventDefault();

    $.ajax( {
      url: 'index.php',
      data: {
        'action': 'something'
      },
      success: function() {
        alert(old_this.data("foobar"));
      }
    });
  });
)};

I can't assign unique IDs to the element being clicked, so accessing it via $("#id") isn't an option.

Is there a more standardized approach to accessing the $(this) that existed before entering the success function or does this way work just fine?

Share Improve this question edited Jun 26, 2015 at 13:46 Sumurai8 20.8k11 gold badges69 silver badges102 bronze badges asked May 29, 2014 at 20:54 OnNIXOnNIX 4424 silver badges10 bronze badges 2
  • maybe by using context:this for the object passed in .ajax()? – King King Commented May 29, 2014 at 20:56
  • You are doing exactly what you should be doing. My only remendation would be to prefix the variable name with a $. This has bee a somewhat mon practice for variables that reference a jQuery object. Ex. $old_this You should name your variable a little better though. – Josh Mein Commented May 29, 2014 at 20:59
Add a ment  | 

1 Answer 1

Reset to default 8

The way that you have it is just fine. By default this in jQuery ajax callbacks is the ajax settings object (you can set via $.ajaxSettings). $.ajax also has a context property that you can set:

$.ajax({
    url: url,
    data: data,
    context: this,
    success: success
});

Then you could use $(this) as expected, but personally I find the reassignment of this easier to understand. You may want to pick a better variable name than old_this, though.

发布评论

评论列表(0)

  1. 暂无评论