如何保存数据在文本区域上的输入键使用ajax像facebook评论


How to save data in textarea on enter key using ajax like facebook comment?

我已经在视图页面和控制器中完成了这样的代码同时按多次回车键将数据保存到表中。谁能帮助解决这个问题?

视图页面

<form>
<div class="cmt-box">
<textarea class="form-control" name="txtArea" id="txtArea<?php echo $row->id;?>" onkeypress="onTestChange(1)" rows="1"></textarea>
</div></form>
脚本

function onTestChange(id) { 
  $("#txtArea"+id).keypress(function(e) {  
   if(e.which == 13) {
dataString=document.getElementById("txtArea"+id).value;
$.ajax({
  type: "POST",
  url: "<?php echo site_url('show/insertcomment'); ?>",
  data: { comment :dataString, id:id},
  success: function(data){
 location.reload();
  }
});
 }
    });

}
$(".class_txtarea").keypress(function(e) {  
   if(e.which == 13) {
     dataString=document.getElementById(this).value;
     $.ajax({
      type: "POST",
      url: "<?php echo site_url('show/insertcomment'); ?>",
      data: { comment :dataString, id:id},
      success: function(data){
          location.reload();
      }
   });
 }

});

HTML:

<div class="cmt-box">
    <textarea class="form-control mytext" name="txtArea" id="txtArea<?php echo $row->id;?>" rows="1"></textarea>
</div>
Jquery:

$(document).ready(function(){
    $('.mytext').keyup(function (evt) {
        evt = evt || window.event;
        if (evt.keyCode == 13) { /* pressed enter key */
            $.ajax({
                type: "POST",
                url: "<?php echo site_url('show/insertcomment'); ?>",
                data: { comment :dataString, id:id},
                success: function(data){
                    location.reload();
                }
            });
        }
    });
});