通过这段代码,我可以在帖子中添加评论。但是有一个问题 - 第一个评论有效,但我无法添加下一个。我使用 livequery,所以它可以工作。你能帮帮我吗?
$('form.comment_form').livequery('submit', function a()
{
var element = $(this);
var wall_post_id = element.attr("id");
var wall_message = $('#comment_text'+wall_post_id).attr('value');
$.post("home/add_comment", $(this).serialize(), function(response)
{
if(response.error == "0")
{
$("div#comments"+wall_post_id).replaceWith(response.message);
}
else if(response.error == "1")
{
alert(response.message);
return false;
}
}, "json");
return false;
});
最佳答案
问题是当您使用 replaceWith 时,它会完全删除该 DOM 元素。因此,它第二次针对第二条评论运行时,不再有匹配的 DIV,因此无需替换任何内容。尝试附加。见以下代码:
$('form.comment_form').livequery('submit', function a()
{
var element = $(this);
var wall_post_id = element.attr("id");
var wall_message = $('#comment_text'+wall_post_id).attr('value');
$.post("home/add_comment", $(this).serialize(), function(response)
{
if(response.error == "0")
{
$("div#comments"+wall_post_id).append(response.message);
}
else if(response.error == "1")
{
alert(response.message);
return false;
}
}, "json");
return false;
});
关于javascript - JQuery - 添加超过 1 条评论不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2389941/