从.done返回值到$ this @jquery ajax

时间:2013-04-01 13:31:04

标签: javascript jquery

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

$this.done子功能中不起作用。我在这做错了什么?

3 个答案:

答案 0 :(得分:6)

因为this没有引用回调中的元素项。

尝试以新值结束。

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);
});
});

答案 1 :(得分:4)

试试这个:

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);
    });
});

答案 2 :(得分:3)

您还可以设置context的{​​{1}}选项, check this option

$.ajax