2014-07-03 43 views
0

我創建一個評論系統,其結構如下圖所示:我想追加textarea的數據給主要的意見(也更新數據庫)追加textarea的數據回車鍵評論發表

<div id="main_comment_DIV_id_no"> 
    //Some user comment 
</div> 
<div "reply_DIV"> 
    <textarea name="reply_comment" id="reply_comment" rows="1" style="width:100%"> 
    </textarea> 
</div> 

部分按下Enter鍵。

我可以很容易地做到這一點,如果僅僅是有一個(或幾個)DIV S,但在這裏我有n數字DIV其實如此我正在尋找一個解決方案,第n個應答數據追加到其中只有第N main_comment_DIV

任何幫助將非常有用?

+0

你在這裏嘗試了什麼? –

+0

添加您的代碼...我們可以嘗試調試它。但創建它完全是你的任務。 – j809

+0

@Ronak和j809感謝您的評論,我只是想知道我應該在這裏應用的邏輯....我試圖運行我的思想,但沒有運氣 – user3478137

回答

0

這是邏輯。這可能會幫助你。 當你點擊enter時,調用ajax函數並傳遞reply_comment。在PHP方面,將該註釋添加到數據庫中並從數據庫獲取所有消息。關於阿賈克斯成功,

$('#main_messages_container').html(null); 
$('#main_messages_container').html(res); 

這將給你所有更新的消息與新的。在這種情況下,您不需要附加任何div容器。

詳細的解答:

$('#enter_key_button').on('click',function(){ 
$.ajax({ 
     type: 'post', 
     url: 'process.php', 
     data: {textarea : textarea}, 
     success: function (res) { 
      //alert(res); 
      $('#main_messages_container').html(null); 
      $('#main_messages_container').html(res); 
     } 
    }); 
}); 
0

您可以使用keyup事件和jQuery類選擇嘗試。你不應該使用id作爲選擇器,因爲你說你有n個主要評論潛水,所以n個div的id不應該相同。

<body> 
    <script type="text/javascript"> 
     $(document).ready(function(){ 
      $('[name="reply_comment"]').keyup(function(e){ 
       if(e.which === 13){ 
        var comment = $(e.currentTarget).val(), 
        commentDiv = $('.main_comment_DIV_id_no'); 
        //you have the comment here, so you can call an ajax request to update it in the database. 
         $(e.currentTarget).val(""); 
        //if the blank div where you want to add the comment is already present, then write 
         $(commentDiv[commentDiv.length-1]).text(comment); 
       // Else no need to add the blank div initially, just create a new div and add the comment there. 
        $(commentDiv[commentDiv.length-1]).after('<div class="main_comment_DIV_id_no">'+comment+'</div>'); 
       } 

      }) 

     }) 

    </script> 
    <div class="main_comment_DIV_id_no"> 
     Some user comment 
    </div> 
    <div class="main_comment_DIV_id_no"> 
     Some user comment2 
    </div> 
    <div class="main_comment_DIV_id_no"> 

    </div> 
    <div id="reply_DIV"> 
     <textarea name="reply_comment" id="reply_comment" rows="1" style="width:100%" ></textarea> 
    </div> 
</body>