我有以下HTML结构:
<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);
}
});
});
答案 0 :(得分: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上的代码示例。
答案 1 :(得分: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);
}
});
});