我正在社交网站上工作。在第一次加载时,它会获取所有帖子,每个帖子最多有两个评论。如果评论超过两个
它会附加More comment
按钮,如果点击它会显示所有评论,并将按钮替换为Less comment
。
这只是一个关于它如何运作的小故事。我希望无论何时发表评论,都应将其附加到现有评论并立即显示 AJAX。它这样做但它重新显示每次评论3次,当你重新加载页面时,一切都很好......
<div class = 'post_updates'>
<div class = 'feeds'>
<div class = 'commentdiv'>
<textarea autocomplete = 'off' class='commenttext form-control' rows = '1'
placeholder='Have your say...'></textarea>
<button class='comment btn btn-xs btn-primary onespacedown' value = '7'
type='submit'>Comment</button>
</div>
</div>
</div>
jQuery AJAX代码:
$('.feeds').on('click', '.comment', function () {
var $this = $(this);
var post_comment = $this.parents('.feeds').find('.commenttext').val();
var post_id = $(this).val();
var user_id = $(".user_id").text();
var request = $.ajax({
url: "insert.php",
type: "POST",
data: { post : post_id , user : user_id, comment: post_comment },
dataType: "html"
});
request.done(function( msg ) {
$pc = $this.parents('.feeds').find('.per_comment');
//fetch comments and display
var request = $.ajax({
url: "comments.php",
type: "POST",
data: {
post: post_id,
user: user_id
},
dataType: "html"
});
request.done(function (msg) {
$pc.html(msg).data('loaded', true);
$this.replaceWith("<button class='lesscomments btn-block pullcomments' value='' name = 'more' type='submit'>Less comments</button>");
$this.parents('.feeds').find('.commenttext').val('');
});
});
})
事先,谢谢。
答案 0 :(得分:0)
在jquery中尝试$ .post
$.post("insert.php",{post : post_id , user : user_id, comment: post_comment},function(){})
.success(function(data){
//callback when first post completed
//begin second ajax post
$.post("comments.php",{post: post_id,user: user_id},function(){})
.success(function(){
//call back when second post completed
});
});