[英]Appending textarea data on pressing Enter key to a comment post
我正在創建一個評論系統,其結構如下:
<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
鍵將textarea數據附加到主要注釋(也更新數據庫)部分。
如果只有一個(或幾個) DIV
,我可以很容易地做到這一點,但是這里我有n
個DIV
所以我正在尋找一種解決方案,將第n個答復數據僅附加到第n個main_comment_DIV
。
任何幫助將非常有用?
這是邏輯。 那可能對您有幫助。 當您單擊enter時,調用ajax函數並與之一起傳遞reply_comment。 在php端,將該注釋添加到數據庫中並從數據庫中獲取所有消息。 在ajax成功的基礎上,
$('#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);
}
});
});
您可以嘗試使用keyup事件和jquery類選擇器。 您不應將id用作選擇器,因為您說自己有n個主要評論潛水,因此id個n個div不應相同。
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.