首页 > 解决方案 > 使用 Thymeleaf 在 Select 上选择选项后如何执行操作?

问题描述

使用 Thymeleaf 在 Select 上选择选项时,我试图重定向我的页面。我的代码是这样写的:

<select>
    <option th:each="size : ${pageSizes}" th:text=${size}  th:onclick="'window.location.href = \'' + @{/ingredient(size=${size})} + '\''"></option>
</select>

而且,在谷歌浏览器上检查后,创建的 HTML 是:

<select>
    <option onclick="window.location.href = '/ingredient?size=5'">5</option>
    <option onclick="window.location.href = '/ingredient?size=10'">10</option>
    <option onclick="window.location.href = '/ingredient?size=20'">20</option>
    <option onclick="window.location.href = '/ingredient?size=25'">25</option>
    <option onclick="window.location.href = '/ingredient?size=50'">50</option>
</select>

But nothing happens when a select a new Option. 我究竟做错了什么?

标签: htmlspring-bootspring-mvcthymeleaf

解决方案


在标签上使用 onchange 事件select而不是 onclick。

<select th:onchange="'window.location.href = \'' + @{/ingredient} + '?size=\' + this.value ' ">
    <option th:each="size : ${pageSizes}" th:text=${size}  ></option>
</select>

推荐阅读