jQuery Ajax两个相同的功能为一个而不是另一个工作

时间:2016-10-26 20:31:27

标签: javascript php jquery ajax

我正在建立一个评论系统,用户可以在帖子上发表评论,然后其他用户可以回复该评论。我已经做了一个评论功能完美的功能。我还想要一个类似的函数来处理注释部分的回复,所以我复制了相同的函数并对代码进行了一些更改,比如变量名等。但它不起作用。当我写回复并按回车键时,它会转到新行,而不是提交。请帮帮我。

// 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 } ?>

两个功能都相同。但是用于评论的一个工作完全正常,而用于回复评论的工作则不起作用。它应该提交表格并在我按照FACEBOOK写一些文字后输入REPLY TO COMMENTS表格时插入数据。请帮忙。

1 个答案:

答案 0 :(得分:0)

你的评论打破了代码:

改变这个:

<!-- Reply to comments form -- >

为此:

 <!-- Reply to comments form -->

并更新。

相关问题