在ajax回调jquery中使用$(this) [英] use $(this) in ajax callback jquery

查看:121
本文介绍了在ajax回调jquery中使用$(this)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对php文件进行jQuery.post,该文件返回的值是我的值.

i'm doing a jQuery.post to a php file, and the file return's me a value.

问题是:为什么$(this)剂量在回调函数中起作用? 使用$(this)的任何警报传递要显示的内容时,返回的是我null

the question is: why the $(this) dosent work in the callback function ? any alert passing something to show, using $(this), return's me null

$(".class").live("focusout", function(){

    jQuery.post("phpfile.php",
       {
           someValue: someValue
       },
       function(data)
       {
             // why the $(this) dosent work in the callback ?
       }                

    )

});

推荐答案

在这种情况下,this不再是同一对象.先保存参考,以后再使用:

In that case this is not the same object anymore. Save a reference before and use later:

$(".class").live("focusout", function(){
    var $this = $(this);
    jQuery.post("phpfile.php",
       {
           someValue: someValue
       },
       function(data)
       {
           // 'this' inside this scope refers to xhr object (wrapped in jQuery object)
           var x = $this;
       }                
    )
});

这篇关于在ajax回调jquery中使用$(this)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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