【问题标题】:How can I perform an action after selecting an Option on a Select using Thymeleaf?使用 Thymeleaf 在 Select 上选择选项后如何执行操作?
【发布时间】:2020-05-05 05:27:54
【问题描述】:

当使用 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>

但在选择一个新选项时,没有任何反应。我做错了什么?

【问题讨论】:

    标签: html spring-boot spring-mvc thymeleaf


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 2019-06-03
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 2013-01-19
      • 2020-06-22
      • 2018-07-15
      • 1970-01-01
      相关资源
      最近更新 更多