2013-10-13 36 views
1

im我的JQUERY有一些问题。如何在JQUERY中添加特定DIV中的数据JQUERY

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

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

我的问题是,每当我在一个特定的POST我做了更新,以ALL我的帖子的评论提交评论。它只会在我刷新时消失。

这是我<div>显示的TITLE评论

<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> 

THIS IS MY JQUERY。每当用户在特定POST命中<enter>应该对特定DIV /帖子显示注释只有

$(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所以每当我打在我的输入框中输入,我的系统知道具体是什么post im即指..

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

回答

0

的问题是在这条线:

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

这样,你就追加了新的注释文本在每个divcomments css类。

你可以像执行的东西:

<!-- 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>"); 
     } 
    }); 
}); 
+0

哦,伙计!这次真是万分感谢! – bobbyjones

+0

不客气;) – rd3n

1

问题在于你试图区分你的评论DIVs的方式。假设你想选择一个特殊的评论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>");