【问题标题】:Thymeleaf foreach loop with button event insideThymeleaf foreach 循环,里面有按钮事件
【发布时间】:2018-11-29 01:31:38
【问题描述】:

我使用 Thymeleaf foreach 遍历所有帖子,其中每个帖子都有一个“评论”按钮。我想在点击这个“评论”按钮后显示评论列表。

The default is hidden

以下是我的代码:

            <div th:each="post:${posts}">
                <div class="panel panel-default" th:object="${post}">
                    <div class="panel-body">
                        <p th:text="*{user.username}">username</p>
                        <p th:text="*{createTime}">time</p>
                        <p th:text="*{content}">content</p>
                        <div>
                            <form th:action="@{/posts/liked/input}" method="post"
                                style="display: inline">
                                <input type="hidden" name="postId" id="postIdId"
                                    class="form-control" th:value="*{id}">
                                <button type="submit" class="btn btn-primary">like</button>
                            </form>
                            <button class="btn btn-primary commentBt"
                                style="display: inline">Comment</button>
                        </div>
                    </div>

                    <!--  This is the part I want to show after click Comment -->
                    <div style="display: none">
                        <form th:action="@{/posts/comment/input}" method="post">
                            <textarea class="form-control" name="content" id="contentId"
                                rows="1"></textarea>
                            <input type="hidden" name="postId" id="postIdId"
                                class="form-control" th:value="*{id}">
                            <button type="submit" class="btn btn-primary">Reply</button>
                        </form>
                        <div th:each="comment:*{comments}">
                            <div th:object="${comment}">
                                <p th:text="*{content}">content</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

如何在 foreach 循环中做到这一点?

【问题讨论】:

    标签: javascript html spring-boot thymeleaf


    【解决方案1】:

    您可以使用js 来做到这一点,下面是示例代码。

    首先,您需要在提交按钮中添加onclick

    <button  class="btn btn-primary commentBt"
                                style="display: inline" onclick="showDiv()">Comment</button>
    

    然后,获取隐藏的div和编辑函数showDiv来显示div。

      <!-- set id -->
      <div id="test" style="display: none">
    
        <script>
        <!-- function to change display to block -->
        function showDiv(){
            var div = document.getElementById("test");
            div.style.display="block";
        }
    </script>
    

    希望对您有所帮助!

    【讨论】:

    • 谢谢。但问题是,由于它在 foreach 循环中,所有
      都具有相同的 id,即“test”。当我点击这个按钮时,它总是会显示第一个
      。我应该为每个
      生成动态 id 吗?
    • 可以,可以使用th:id生成动态id。然后你可以重写函数showDiv,它的参数为id,像这样:showDiv(div_id)。点击提交按钮时,需要将参数传递给函数showDiv
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签