2016-12-23 18 views
1

我想在ajax成功函數中使用$(this)選擇器來選擇之前雙擊過的元素.selectclass。但是,這不起作用。是否有可能在ajax成功函數中選擇?

的jQuery:

$(document.body).on("dblclick",".selectclass",function(){ 
    bla = $(this).attr("class").slice(5); //works fine 
    $.ajax({ 
     url: 'bla.php', 
     type: 'post', 
     dataType:'json', 
     data: { 
      'bla':bla 
     }, 
     success: function(reply) { 
      if((reply == "blabla") 
      { 
       $(this).siblings().find(".likes, .likes1").slideToggle('slow'); //not working 
      } 
     } 
    }); 
}); 

回答

3

需要緩存的變量。

$(document.body).on("dblclick", ".selectclass", function() { 
    var $this = $(this); 
    var bla = $this.attr("class").slice(5); //works fine 
    $.ajax({ 
    url: 'bla.php', 
    type: 'post', 
    dataType: 'json', 
    data: { 
     'bla': bla 
    }, 
    success: function(reply) { 
     if (reply == "blabla") { 
     $this.siblings().find(".likes, .likes1").slideToggle('slow'); // will work now 
     } 
    } 
    }); 
}); 

請使用var作爲局部變量。您還有一個錯字if

if((reply == "blabla") // Remove the double parenthesis. 
+2

您使用它之前,您不妨緩存,但+1 – DelightedD0D

+1

@ DelightedD0D這條線是OP正確測試線,所以我忽略了它。對? ':)' –

+2

請使用'var'! –

相關問題