[英]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.