2011-03-15 3 views
0

je la structure HTML suivant:post succès jQuery retour au conteneur parent

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

Je suis Tring pour obtenir la réponse à afficher dans la durée de parent, mais je ne peux pas le comprendre.

$('.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);     
     } 
    }); 
}); 

Répondre

7

Vous devez capturer l'élément à l'intérieur de votre fonction de clic. $(this) intérieur du rappel de succès sera le référencement l'objet ajax() et non votre 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);     
     } 
    }); 
}); 

Exemple de code sur jsfiddle.

+0

Me battre: D –

1

essayez ceci:

$('.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);     
    } 
}); 

});