2014-01-23 40 views
0

textarea的形式數據提交給服務器,但它無需刷新頁面發佈評論時不使用Ajax刷新頁面?

這裏沒有顯示註釋是腳本:

$("#post_reply").click(function(event) { 
    event.preventDefault(); 
    if(document.getElementById('_comment').value.trim()==""){ 
     return false; 
    } 
    $.post('../services/leave_comment.php', $("#open_status").serialize(), 
     function(data) { 
      $('#ajax_loading').hide(); 
      if(data){ 
       $.ajax({ 
        type: 'POST', 
        url : 'http://localhost/tech1/services/get_more_comments.php', 
        data: 'last_id='+last_id, 
        success: function(data){ 
         $('.view_container').append(data); 
         $('.view_container_parent').load('get_more_comments.php'); 
        }, 
        complete: function(){ 
         console.log('DONE'); 
        }      
       }); 
      } 
     }); 
}); 

這裏是html和view_container_parent的結構已經被加載而無需刷新

<div class="comments" id="comments"> 
    <div class="comm_container"> 
     <div class="insert_container"> 
      <form class="commentform"> 
      <textarea> 
      </form> 
     </div> 
    </div>   
    <div class='view_container_parent'>//whole lot of comments 
     <div class='view_container'>//single comment 
     </div> 
     <div class='view_container'>//single comment 
     </div> 
     <div class='view_container'>//single comment 
     </div> 
    </div> 
</div> 
+0

測試響應數據請幫助我,,,,這已經吃了我的三天。 – stacky

+0

您是否嘗試過使用'html()'來放置內容? – PRAISER

+0

在Chrome中,右鍵單擊,檢查元素,單擊網絡選項卡。請求是否被髮送到服務器,數據是否按預期返回? – Calebj

回答

2

試試這個:

$("#post_reply").click(function(event) { 
    event.preventDefault(); 
    if(document.getElementById('_comment').value.trim()==""){ 
      return false; 
    } 
    $.post('../services/leave_comment.php', $("#open_status").serialize(), 
     function(data) { 
      $('#ajax_loading').hide(); 
      if(data){ 
       $('#ajax_loading').show(); 
       $.ajax({ 
       type: 'POST', 
       url : 'http://localhost/tech1/services/get_more_comments.php', 
       data: 'last_id='+last_id, 
       success: function(data) { 
        $('#ajax_loading').hide(); 
        console.log('AJAX SUCCESS'); 
        $('.view_container_parent').append(data); 
        console.log('Append data'); 
        console.log(data); 
       }, 
       complete: function() { 
        console.log('DONE'); 
       }     
       }); 
      } 
     } 
    ); 
}); 

你的反應必須是(環):

<div class="view_container"> 
... 
</div> 

你的HTML只需要:

<div class="view_container_parent"> 
    <div class="view_container"> 
     <!-- Another comment before --> 
    </div> 
    <!-- The ajax will append data from here --> 
</div> 

您可以console.log(data)

+0

是的,我認爲append應該用在'view_container_parent'類上。如果只想要新的評論出現,我認爲沒有必要做另一個AJAX調用 – Gnijuohz

+0

我不知道'../ services/leave_comment.php'的發佈請求。我認爲它用於提交textarea,如果提交成功,那麼下面的ajax將工作並追加新的評論。 –

+0

'leave_comment.php'是將記錄插入到數據庫中 – stacky