1

我的 JQUERY 遇到了一些问题。

基本上,我有这种显示帖子的 facebook 风格。每个帖子都有<input>框,成员可以在其中发表评论。当用户点击<enter>我的 jquery(AJAX) 时,将获取提交的评论并将其保存在我的数据库中。

评论应立即出现在发表评论的特定<DIV>位置。

我的问题是,每当我在特定帖子上提交评论时,所做的评论都会更新到我的所有帖子。它只会在我点击刷新时消失。

这是我的<div>,显示标题评论

<div id="content">
    <?php 
    /* GET TITLE*/
    $result = displayPosts(); 
        while ($row = $result->fetch_assoc()) { 
        $rowId = $row['id'];

    ?>
    /* ECHO TITLE*/     
    <b> <?php echo $row['title'] . "<br />"; ?> </b>

    <?php   
    /* GET COMMENTS PER POSTS */
    $comRes = getComments($rowId);
        while ($comRow = $comRes->fetch_assoc()) { 
    ?>

    <!--COMMENT AREA-->
    <div class="comments">
        <!--DISPLAY COMMENTS-->
        <small> <?php echo $comRow['comment'] . "<br />";?> </small>
    </div>

    <?php } /* end of second while loop */ ?> 

    <input type="text" post_id="<?php echo $row['id']; ?>" id="TextBox" name="input" value="" />

    <?php } /* end of first while loop */ ?> 
</div>

这是我的 JQUERY。每当用户点击<enter>特定的 POST 时,它应该只显示对该特定 DIV/POSTS的评论

$(document).ready(function() {
    $('input#TextBox').keyup(function (z) {
        /* when this commentbox is entered*/
        if(z.keyCode == 13) {   /*keyCode 13 = enter*/ 
                var post_id = $(this).attr('post_id');  /* get the post_id in the <input> box */
                var comment = $(this).val(); /* get the value of the <input> */
                $.post('../portal/comment.php', {post_id: post_id, comment: comment});
                $('input#TextBox').val('');
                $(this).parent('#content').children('.comments').append("<div>"+comment+"</div>");
        }
    });
});

此行包含我的post_id所以每当我在输入框中按 Enter 键时,我的系统就知道我指的是什么特定的帖子。

<input type="text" post_id="<?php echo $row['id']; ?>" id="TextBox" name="input" value="" />
4

2 回答 2

1

问题在于您尝试区分评论 DIV 的方式。假设你想选择一个特殊的评论 div。你怎么能在你的网页上做到这一点?使用此代码不会给您一个特殊的注释 div:

$(".comments")

您应该给每个评论 DIV 一个特殊的身份(它只是一个 html id)。这样您就可以轻松选择它,例如:

$("#comments_14")

并且更新会变得更加复杂。而不是以下行:

$(this).parent('#content').children('.comments').append("<div>"+comment+"</div>");

你应该这样做:

var post_id = get post id some way; // e.g. put it in an attribute in text input
$('#comments_' + post_id).append("<div>"+comment+"</div>");
于 2013-10-13T13:20:30.490 回答
0

问题出在这一行:

$(this).parent('#content').children('.comments').append("<div>"+comment+"</div>");

div有了这个,您将在每个带有commentscss 类的内容中附加新的注释文本。

您可以执行以下操作:

<!-- Add post id to identify corresponding comment area -->
<div class="<?php echo $rowId; ?>_comments comments"> 
    <small> <?php echo $comRow['comment'] . "<br />";?> </small>
</div>

然后在你的 js 中:

$(function() {
    $('input#TextBox').keyup(function (z) {
        if(z.keyCode == 13) {
            var input = $(this);
            var post_id = input.attr('post_id');
            var comment = input.val();
            $.post('../portal/comment.php', {post_id: post_id, comment: comment});
            $('input#TextBox').val('');
            // Append comment to the corresponding div
            $('.' + post_id + '_comments').append("<div>"+comment+"</div>");
        }
    });
});
于 2013-10-13T13:24:53.003 回答