如何在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
相关产品推荐
相关产品推荐

