use $(this) in ajax callback jquery

后端 未结 2 1545
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 01:06

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

the question is: why the $(this) dosent work in the callback fun

相关标签:
2条回答
  • 2020-12-02 01:13

    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;
           }                
        )
    });
    
    0 讨论(0)
  • 2020-12-02 01:29
    $(".class").live("focusout", function(){
        var this = $(this);
        jQuery.post("phpfile.php",{
           someValue: someValue
       },function(data){
            // Now use this instead of $(this), like this.hide() or whatever.
       })
    });
    

    $(this) in your example was refering to the $.post i think.

    0 讨论(0)
提交回复
热议问题