You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Thymeleaf的select组件中将选中行值参数传递给jQuery函数

解决方案

你当前代码的问题在于data-test属性是绑定在<option>子元素上的,而this在select的onchange事件中指向的是<select>元素本身,直接调用getAttribute只能获取到select标签自身的属性,无法拿到选中项的自定义属性。

你只需要修改select标签的th:onchange属性取值逻辑即可,不需要调整其他部分的代码:

<select th:onchange="changes(this.options[this.selectedIndex].getAttribute('data-test'))">
  <option th:each="list : ${allList}"
          th:value="${list?.number}" 
          th:text="${list?.name}"
          th:data-test="${list?.test}">
  </option>
</select>

这里的this.options[this.selectedIndex]会直接获取到当前选中的<option>元素,再调用getAttribute就能拿到对应绑定的list.test值传入changes函数。

内容的提问来源于stack exchange,提问作者DB K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 04:57:00