首页 > 解决方案 > 评论和子评论形式:在while循环(php)中显示和隐藏它并基于jquery脚本逻辑时出现问题

问题描述

问题在于我的两种形式(用于用户的评论和子评论)。我已经实现了一个 jquery 代码,以便在单击“回复”按钮时可以显示或隐藏这种表单。好吧,伙计,到目前为止一切都很好。但经过几次测试后,我注意到这对评论有效,但对子评论无效。

它适用于子评论,但仅适用于我发布的子评论以及我登录自己的系统时。在其余的子评论中,它不起作用,当我单击辅助评论或子评论“回复”按钮时,它不会显示或隐藏任何内容,并且其他按钮(如“删除”和“编辑”)看起来不活动并且根本不工作。嗯,这是我的第一个问题。现在,我的第二个问题是跨度“查看回复”。

当我单击它时,它会立即隐藏所有具有类“.replies”的“ul”(无序列表)(其中有主评论的子评论)而不是一一隐藏(隐藏和显示切换)我想要。此外,当我再次单击它时,它根本不会重新显示。这是 jQuery 代码:

$(document).ready(function(){
  $(document).on('click' , '.reply' , function(){
     var closestDiv = $(this).closest('div'); // also you can use $(this).parent()
     //closestDiv.fadeOut();
     $('.replyto').not(closestDiv.next('.replyto')).hide();
     //$('.reply').closest('div').not(closestDiv).show()
     closestDiv.next('.replyto').slideToggle(100);
  });
});

上面这个是切换评论的“回复”形式。它适用于第一级(评论),但不适用于子评论。

$(document).ready(function(){
  $(document).on('click' , '.clicktoviewreplies' , function(){
     var closestUl = $(this).closest('ul'); // also you can use $(this).parent()
     //closestUl.fadeOut();
     $('.replies').not(closestUl.next('.replies')).hide();
     //$('.clicktoviewreplies').closest('ul').not(closestUl).show()
     closestUl.next('.replies').slideToggle(100);
  });
});

上面的这个是切换子评论或回复。类似于“显示回复”和“隐藏回复”。这是整个 php 代码。如您所见,它基于四个表:registered_users、video(尽管不在此处)、comments 和 subcomments。

<?php
while($commentslist = $showcomments->fetch()){
$iduser = $commentslist['idUser'];
$idcomment = $commentslist['id'];
$showuser = $pdo->prepare("SELECT * FROM registered_users WHERE id = ?");
$showuser->execute(array($iduser));

while($row6 = $showuser->fetch()){

<li>
<div>
    <b><?php echo $row6['username']; ?></b><br>             
    <?php echo $commentslist['comments']; ?>

<!--additional comment buttons or controllers-->
    <?php
    if($iduser == $row['id']){ ?> <button>Edit</button><?php }else{ ?> <button>Report</button><?php } ?> <button class="reply">Reply</button><?php if($iduser == $row['id']){ ?> <a onClick="return confirm('You sure you wanna delete that comment there')" href=""><button>Delete</button></a><?php }
    ?>
</div>

<div class="replyto">
    <!--THIS IS TO SUBMIT THE CHILD COMMENTS(SUBCOMMENTS)-->
    <form enctype="multipart/form-data" method="post" action="<?php echo "postsubcomment.php?id=$idcomment"; ?>">
        <textarea name="subcomment" cols="50" rows="4"></textarea>
        <input type="submit" name="reply" value="Submit">
    </form>
</div>
<span class="clicktoviewreplies"><b>View replies</b></span> <!--ATTENTION!HERE IT IS WHERE MY PROBLEM IS. I CLICK ON IT AND IT HIDES ALL "UL"(Unordered Lists) WITH CLASS ".REPLIES" AND NOT ONE BY ONE (SWITCHING) LIKE I'D LIKE. AND WHEN I CLICK ON IT AGAIN, IT DOESN'T RESHOW.-->
<!--SECOND LEVEL UNORDERED LIST-->
    <ul class="replies">
<!--Subcomments go/show here. This happens everytime the user replies to a main comment-->
    <?php
    $showsubcomments = $pdo->prepare("SELECT * FROM subcomments WHERE idComment = ?");
    $showsubcomments->execute(array($idcomment));
    while($subcommentslist = $showsubcomments->fetch()){
        $subcommenter = $subcommentslist['idComment'];
        $selectcomment = $pdo->prepare("SELECT * FROM comments WHERE id = ?");
        $selectcomment->execute(array($subcommenter));
        $row7 = $selectcomment->fetch();

        //This is to get the parent comment user
        $selectuser = $pdo->prepare("SELECT * FROM registered_users WHERE id = ?");
        $selectuser->execute(array($row7['idUser']));
        $row8 = $selectuser->fetch();

        $subcommentslist['idUser']; //The user that made the subcomment
        $selectuser2 = $pdo->prepare("SELECT * FROM registered_users WHERE id = ?");
        $selectuser2->execute(array($subcommentslist['idUser']));
        $row9 = $selectuser2->fetch();

?>
<!--SECOND LEVEL LIST ITEM: This is to show the subcomments-->
        <li>
        <div>   
            <?php echo "<b>".$row9['username']; ?>             
            <?php echo "<br>".$subcommentslist['subcomments']."<br>"; ?> 
            <!--additional subcomment buttons or controllers-->
            <?php
            if($row9['id'] == $row['id']){ ?> <button>Edit</button><?php }else{ ?> <button>Report</button><?php } ?> <button class="reply">Reply</button><?php if($row9['id'] == $row['id']){ ?> <a onClick="return confirm('You sure you wanna delete that comment there')" href=""><button>Delete</button></a><?php }
            ?>                                   
        </div>
        <div class="replyto" style="margin-left:5%; position:relative;">
            <!--THIS IS TO REPLY TO BOTH THE MAIN COMMENT AND SUBCOMMENTS BELOW IT-->
            <form enctype="multipart/form-data" method="post" action="<?php echo "postsubcomment.php?id=$idcomment"; ?>">
                <textarea name="subcomment" cols="50" rows="4"></textarea>
                <input type="submit" name="reply" value="Submit">
            </form>
        </div>
        </li>
<?php               
            }//THIRD (SUBCOMMENTS) WHILE CLOSED
        ?>
    </ul> <!--SECOND UNORDERED LIST CLOSED -->
</li>
<?php
} //SECOND WHILE CLOSED
} //FIRST WHILE CLOSED
?>

好吧,第一个实例(评论)“回复按钮”的行为有效。为什么不是第二个实例(回复子评论)?我想知道问题是第二个还是第三个 while 循环,或者它可能与“ul”和“li”的安排有关。

标签: phpjquerytoggleshow-hidecomments

解决方案


那么,这里的老板会为你解决这些问题,伙计。对于第二个问题(跨度:“查看回复”),将您的代码更改为:

$(document).ready(function(){
     $(".clicktoviewreplies").click(function(){
    //$(this).closest('ul').next('.replies').hide();
    //$('.replies').
    $('.replies').not($(this).next('.replies')).css("display", "none");
    $(this).next('.replies').slideToggle(100);
    }); 
});

并将文本更改为“显示/隐藏回复”。至于第一个问题(回复按钮和评论和“子评论”表单),删除“style”标签和所有css,除了属性“display:none”。请记住,jQuery 甚至 Javascript 代码可能会出现故障,因为您使用了类本身之外的东西来识别代码中的元素。我不知道如何解释为什么会发生这种情况。所以不要使用:

<div class="replyto" style="margin-left:5%; position:relative;">

利用:

<div class="replyto">

在 CSS 中,仅使用:

    .replyto
{
   display: none;
}

还有一个提示:为了获得结构良好且“不稳定”的代码实现方式,您可以避免使用“样式”标签,而仅使用在单独的样式表文档中组织的 CSS。我希望对你有所帮助,伙计。


推荐阅读