jQuery Ajax的两个相同的函数为其中一个工作,而不是另一个


jQuery Ajax two identical functions working for one and not the other

我正在制作一个评论系统,其中用户可以对帖子发表评论,然后其他用户可以回复该评论。我编写了一个注释函数,它可以完美地工作。我还想要一个类似的函数来回复评论部分,所以我复制了相同的函数,并对代码进行了一些更改,如变量名等。但这行不通。当我写回复并按回车键时,它会把我带到新的一行,而不是提交。请帮帮我。

// COMMENT UPDATE (WORKING)
$(document).on('keydown', '.commentarea', function(event) {
    var comtt = $(".commentarea").val();
    if(comtt!=''){
        if(event.keyCode == 13 && !event.shiftKey) {
            var id = $(this).attr('id');
            var status_id = id.replace("postcomment_", "");
            createComment(status_id);
            event.preventDefault();
        }
    }
});
function createComment(status_id){
  var comment = $('#postcomment_'+status_id).val();
  $.ajax({
      url: "post-comment.php",type: "POST",
      data: {comment : comment, statsid : status_id},
      success: function (data) {
        $("#showbox_"+status_id).append(data);
      },
      error: function () {
        alert("Ooops!! Problem Ocurred. Please try again later. If problem persists, please contact support!");
      },
      complete: function(xhr) {
        $('#postcomment_'+status_id).val('');
      }
  })
}
// REPLY TO COMMENT (SAME FUNCTION BUT NOT WORKING)
$(document).on('keydown', '.replyarea', function(event) {
    var comtr = $(".replyarea").val();
    if(comtr!=''){
        if(event.keyCode == 13 && !event.shiftKey) {
            var rid = $(this).attr('id');
            var commentId = rid.replace("replycomment_", "");
            createReply(commentId);
            event.preventDefault();
        }
    }
});
function createReply(commentId){
  var creply = $('#replycomment_'+status_id).val();
  $.ajax({
      url: "post-replies.php",type: "POST",
      data: {creply : creply, cmtid : commentId},
      success: function (data) {
        $("#showreply_"+commentId).append(data);
      },
      error: function () {
        alert("Ooops!! Problem Ocurred. Please try again later. If problem persists, please contact support!");
      },
      complete: function(xhr) {
        $('#replycomment_'+commentId).val('');
      }
  })
}
PHP部分:

<?php while($get_stf = $get_stq->fetch()){ extract($get_stf); // fetch posts ?>
<?php while($fetch_cmts = $get_cmtq->fetch()){ extract($fetch_cmts); // fetch comments?>
 <!-- Reply to comments form -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="full-width" id="cmt_form_id_<?php echo $cmt_id; ?>">
<input type="hidden" name="comment_id" value="<?php echo $cmt_id; ?>" id="cmtsid_<?php echo $cmt_id; ?>" />
<textarea name="reply" placeholder="Give a reply..." class="reply-comment-field replyarea" id="replycomment_<?php echo $cmt_id; ?>"></textarea>
</form>
<!-- End Reply to comments form -->
<?php } ?>
<!-- Comment form -->
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="full-width" id="cmt_form_id_<?php echo $cmt_id; ?>">
<input type="hidden" name="comment_id" value="<?php echo $cmt_id; ?>" id="cmtsid_<?php echo $cmt_id; ?>" />
<textarea name="reply" placeholder="Give a reply..." class="reply-comment-field replyarea" id="replycomment_<?php echo $cmt_id; ?>"></textarea>
</form>
<!-- End Comment form -->
<?php } ?>

两个函数是相同的。但是用于评论的一个工作完美,而用于回复评论的一个不工作。它应该提交的形式,并插入数据时,我按enter在REPLY TO COMMENTS表单后写一些文本,就像FACEBOOK。请帮助。

你的注释被打破了代码:

改变:

<!-- Reply to comments form -- >

:

 <!-- Reply to comments form -->

和更新。