與Facebook評論系統一樣,當您評論時,您的評論將被添加到之前的評論中並一次顯示。這就是我想在這裏實現的。儘管註釋會被添加,但在顯示之前,必須重新加載頁面。AJAX內的AJAX無法按預期工作?
注意:每個帖子都只加載了兩個評論和一個按鈕,這將允許您加載更多,然後附上。點擊按鈕時,它會通過AJAX加載剩餘的評論,並使用Less comments
更改More comments
按鈕,單擊該評論時可將評論減少爲兩個。
如果有評論,我希望它能像Facebook一樣工作,同時如果用戶沒有點擊More comments
按鈕,它應該會自動顯示剩餘的評論和用戶剛纔評論的新評論。
的HTML:
<div class='feeds'>
<div class='post'>
<h5> Post Header </h5>
<p> post 1 2 3 </p>
<a href ='#' class = 'comment'>Comment</a>
<div class='comments'>
<div class='comment_data>
<div class = ' per_comment '>
<p> Comment 1</p>
</div>
<div class = 'per_comment '>
<p> Comment 2</p>
</div>
<button class='morecomments ' value='7' type='submit '>
More comments</button>
<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> <!-- end of comment_data div -->
</div> <!-- end of post div -->
而這個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: "insertcomment.php",
type: "POST",
data: { post : post_id , user : user_id, comment: post_comment },
dataType: "html"
});
request.done(function(msg) {
var $pc = $this.parents('.feeds').find('.comment_data');
var $button = $this.prev('.pullcomments');
//if the comments are already loaded then don't load it again just display it
var request = $.ajax({
url: "comments.php",
type: "POST",
data: {
post: post_id,
user: user_id
},
dataType: "html"
});
request.done(function (msg1) {
html(msg1).appendTo($pc);
$button.slideDown();
$this.replaceWith("<button class='lesscomments pullcomments' value='7' name = 'more' type='submit'><span class='glyphicon glyphicon-chevron-up'></span></button>");
$this.parents('.feeds').find('.commenttext').val('');
});
});
})
呃。嵌套功能。將第二個ajax調用移到一個函數中,並在你的request.done事件中調用該函數(而不是在'request.done'事件中包含所有代碼*)。 –
你的哪部分代碼不起作用? – Jivings
@Jivings我說,它附加,但不顯示評論,直到你刷新 – Yax