繁体   English   中英

thymeleaf 如何将 id 从下拉列表传递到另一个 html

[英]thymeleaf how to pass an id from dropdown list to another html

建筑物是一个列表,我想从其他 html 中获取选定的列表元素 id 以像这样重定向(/energy/ + building.id)

                    <div th:each="building: ${buildings}" class="list-group-item list-group-item-action">
                       <span class="d-flex align-items-center">
                          <a class="nav-link target-building" th:data-building-id="${building.id}" href="#" id="topBuildingId">
                          <span class="text-sm" th:text="${'&nbsp; ' + building.name}"></span>
                          </a>
                       </span>
                    </div>

和另一个 html 通过 building.id

                    <li class="sidebar-subnav-header">energy data</li>                        
                    <li th:class="${menu=='buildings'} ? 'active' : ''">
                       <a th:href="@{'/energy/' + ${building.id}}">
                          <span>energy data</span>
                       </a>
                    </li>

那么我怎样才能得到一个选定的索引列表ID? 不显示整个列表。

首先,如果您想获得点击的元素 id,您可以通过创建自定义 js function 来实现这一点

function getId(id) {
   alert("clicked element id", id);
}

其次,在需要的地方添加 function。 在你的情况下:

<a class="nav-link target-building" th:data-building-id="${building.id}" href="#" id="topBuildingId" onClick="getId(this.id)">

另外,如果您想使用 javascript 重定向新页面,您可以使用

window.location.href = "URL";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM