使用jQuery和amp;回复集成的评论系统PHP

时间:2015-10-19 11:57:04

标签: comments reply

我目前正致力于评论系统项目。我可以编写注释,将其保存到我的数据库中,然后将其检索并显示在网页上。但是当我在现有的评论系统上加入回复评论功能时,我遇到了麻烦。问题1:我在每个评论div下创建了2个新点击,fisrt是#of; no.of replys',显示新的回复。第二个是现在回复',这是用户回复该评论的文本区域。但是,当我点击其中任何一个时,所有可点击的内容都会在其余的注释div中打开,而不仅仅是我点击的那个。问题2:我只能在最近的评论div上回复并将我的回复发送到数据库。当我尝试回复较旧的评论时,页面会自行刷新,并且没有任何记录到数据库中。以下是我的加价。如果这令人困惑,我很抱歉,但英语不是我的第一语言。任何意见都将不胜感激。

<div class="timeline">

    <?php $stmt = $DB->query("CALL get_comment($aid)");
      $results = $stmt->fetchAll(); $stmt->closeCursor();
      foreach ($results as $info) {
      ?>

        <div class="commentBox">
            //The comment markup

             <p class="allreplys">Replys</p> //This opens all replys
             <p class="replyButton">Reply me</p> //This opens reply form

           <form class="replySection">
           //The reply form markup
           </form>

          <div class="replyTimeline">
            <?php
               $stmt = $DB->query<"CALL get_reply('$aid','$pid')");
               $replys = $stmt->fetchAll(); $stmt->closeCursor();
               foreach ($replys as $re) {
               ?>
                    <div class="replyBox>
                      //Reply Markups
                    </div>
               <?php } ?> //Close of reply foreach
           </div> //Close of replyTimeline

       </div> //Close of commentBox

   <?php } ?> //Close of comment foreach

</div> //Close of timeline

以下是我的jQuery标记

$(document).ready(function() {
    $('.allreplys').click(function(event) {
       $('.replyline').toggle();
    });
    $('.replyButton').click(function(event) {
       $('.replySection').toggle();
    });
});

1 个答案:

答案 0 :(得分:0)

这会更好吗?

我的目标是缩小您正在使用的选择器,该选择器目前使用类名称选择整个页面的所有元素&#34; allreplys&#34;或&#34; replyButton&#34;。使用$(this).parent(&#34; .commentBox&#34;)。find(...

$(this)指的是点击的按钮,在本例中是一个p标签。 &#34;父(...)&#34;导航节点树以获得具有类名&#34; .commentBox&#34;的标签。并且find(...)将在该节点及其子节点(以及它们的子节点等等(递归))中搜索具有选择器的元素。希望只切换一个项目而不是全部。

虽然我没有看到一个名为&#34; replyline&#34;在您的代码中,这可能是一个失败点。

$(document).ready(function() {
    $('.allreplys').click(function(event) {
       $(this).parent(".commentBox").find(".replyline").toggle();
    });
    $('.replyButton').click(function(event) {
       $(this).parent(".commentBox").find(".replySection").toggle();
    });
});