我的 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="" />