2011-03-15 25 views
0

我有以下HTML結構:jQuery的成功後回父容器

<span class="spam"> 
    <button class="spam_button" value="2">Spam</button> 
</span> 

我特林得到父跨度顯示的反應,但我不能弄明白。

$('.spam_button').live('click', function() { 
    var spam_id =$(this).val(); 
    $.ajax({ 
     type: "POST", 
     url: "spam.php", 
     data: "spam_id="+spam_id, 
     success: function(html){ 
      $(this).parent().html(html);     
     } 
    }); 
}); 

回答

7

您需要捕獲點擊函數中的元素。 $(this)內成功回調將引用ajax()對象,而不是你的button

$('.spam_button').live('click', function() { 
    var $button = $(this); 
    var spam_id =$button.val(); 
    $.ajax({ 
     type: "POST", 
     url: "spam.php", 
     data: "spam_id="+spam_id, 
     success: function(html){ 
      $button.parent().html(html);     
     } 
    }); 
}); 

jsfiddle代碼示例。

+0

打我吧:D –

1

試試這個:

$('.spam_button').live('click', function() { 
var $this = $(this); 
var spam_id =$this.val(); 
$.ajax({ 
    type: "POST", 
    url: "spam.php", 
    data: "spam_id="+spam_id, 
    success: function(html){ 
     $this.parent().html(html);     
    } 
}); 

});