2013-12-23 154 views
1

我在社交网站上工作。在第一次加载时,它会提取所有帖子,每个帖子都有两个注释最大值。如果评论超过两个 它附加More comment按钮,如果点击,它会显示所有评论,并用Less comment替换按钮。AJAX内的AJAX无法正常工作

这只是它的一个小故事。我希望每当发表评论时,它都应附加到现有的评论中,并通过 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(''); 
     }); 
    }); 
}) 

在前进,谢谢。 jQuery中

+0

@Tats_innit:jQuery.ajax处理继续回应:“成功:” VS“.done”(http://stackoverflow.com/q/8840257/1438393) –

+0

@Tats_innit'success'与'done'完全相同... – Yax

+0

Agreed @AmalMurali我错过了第二个电话:http://stackoverflow.com/questions/8847829/what-is-difference-between-success - 完成方法的AJAX –

回答

0

试$。员额

$.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 
    }); 
}); 
+0

上面的代码有什么区别? – Yax