【问题标题】:jQuery Ajax two identical functions working for one and not the otherjQuery Ajax 两个相同的功能适用于一个而不是另一个
【发布时间】:2017-03-09 08:53:24
【问题描述】:

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

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

这两个功能是一样的。但是用于 COMMENTS 的一个运行良好,而用于 REPLY TO COMMENTS 的一个却不工作。它应该提交表单并在我像 FACEBOOK 一样编写一些文本后在回复评论表单中按 Enter 键时插入数据。请帮忙。

【问题讨论】:

  • 你可以区分脚本的工作和不工作的地方??
  • 查看代码.. 更新了注释说明哪个工作正常,哪个不工作.. 还请分别查看表格以诊断错误.. 提前感谢伙计:)
  • 您是否复制了错误的评论html?与回复表单相同。
  • 很抱歉,我们不是错误检测器(调试器);如果你能告诉那段代码发生错误并解决不知道:我们可以一起修复它。你最好提出你的问题。
  • @BenG 是的,我也复制了表单.. 但我更改了其中使用的属性值

标签: javascript php jquery ajax


【解决方案1】:

您的评论被破坏了代码:

改变这个:

<!-- Reply to comments form -- >

为此:

 <!-- Reply to comments form -->

并更新。

【讨论】:

  • 你应该使用 cmets 部分这么说..这不是一个答案哥们...
猜你喜欢
  • 2019-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 1970-01-01
相关资源
最近更新 更多