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

javascript - return value from .done to $this @ jquery ajax - Stack Overflow

programmeradmin3浏览0评论
control_td.each(function(){
$.ajax({
  url: 'control.php?udid='+$(this).attr('udid'),
  cache: false,
  async: true
}).done(function(data) {
  $(this).html(data);
});
});

but $this doesn't work in .done sub function. what am I doing wrong here?

control_td.each(function(){
$.ajax({
  url: 'control.php?udid='+$(this).attr('udid'),
  cache: false,
  async: true
}).done(function(data) {
  $(this).html(data);
});
});

but $this doesn't work in .done sub function. what am I doing wrong here?

Share Improve this question edited Apr 1, 2013 at 13:40 Nazik 8,44027 gold badges79 silver badges126 bronze badges asked Apr 1, 2013 at 13:31 user2061745user2061745 3052 gold badges3 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Its because this doesn't refer to the element item in the callback.

Try closing around a new value.

control_td.each(function(){
var $self = $(this); // magic here!
$.ajax({
  url: 'control.php?udid='+$(this).attr('udid'),
  cache: false,
  async: true
}).done(function(data) {
  $self.html(data);
});
});

Try this:

control_td.each(function () {
    var $this = $(this);
    $.ajax({
        url: 'control.php?udid=' + $this.attr('udid'),
        cache: false,
        async: true
    }).done(function (data) {
        $this.html(data);
    });
});

You could also set the context option of the $.ajax, check this option.

control_td.each(function(){
  $.ajax({
    url: 'control.php?udid='+$(this).attr('udid'),
    cache: false,
    async: true,
    context: this
  }).done(function(data) {
    $(this).html(data);
  });
});
发布评论

评论列表(0)

  1. 暂无评论