jQuery的AJAX调用:$(本),成功后不工作 [英] Jquery AJAX call: $(this) does not work after success

查看:140
本文介绍了jQuery的AJAX调用:$(本),成功后不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很奇怪,为什么$(本)jQuery的AJAX调用后不能正常工作。

I am wondering why $(this) does not work after a jQuery ajax call.

我的code是这样的。

My code is like this.

$('.agree').live("click", function(){  // use live for binding of ajax results
      var id=($(this).attr('comment_id'));
      $.ajax({
    	type: "POST",
    	url: "includes/ajax.php?request=agree&id="+id,
    	success: function(response) {
    		$(this).append('hihi');
    	}
      });
      return false;
    });

为什么犯规的$(本)工作AJAX调用后,这种情况下?它会工作,如果我在阿贾克斯,但之后没有效果才使用它。

Why doesnt the $(this) work in this case after ajax call? It would work if I use it before the ajax but no effect after.

推荐答案

在一个jQuery Ajax回调,这是一个参考的AJAX请求中使用的选项。这不是一个引用DOM元素。

In a jQuery ajax callback, "this" is a reference to the options used in the ajax request. It's not a reference to a DOM element.

您需要捕获的天外的 $(本)第一:

You need to capture the "outer" $(this) first:

$('.agree').live("click", function(){  // use live for binding of ajax results
      var id=($(this).attr('comment_id'));
      var $this = $(this);
      $.ajax({
        type: "POST",
        url: "includes/ajax.php?request=agree&id="+id,
        success: function(response) {
                $this.append('hihi');
        }
      });
      return false;
    });

这篇关于jQuery的AJAX调用:$(本),成功后不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆