请写出 thymeleaf获取后端Integer列表数据回显在下拉框中
时间: 2023-04-11 18:01:02 浏览: 69
可以使用th:each遍历Integer列表,将每个元素作为下拉框的选项,使用th:selected属性判断哪个选项被选中,示例代码如下:
<select>
<option th:each="num : ${integerList}"
th:value="${num}"
th:text="${num}"
th:selected="${num == selectedNum}">
</option>
</select>
其中,integerList是后端传来的Integer列表,selectedNum是当前选中的数字。
相关问题
请写出 thymeleaf获取后端列表数据回显在下拉框中
可以使用th:each遍历后端传递的列表数据,将每个数据项添加到下拉框中,使用th:selected属性判断哪个选项应该被选中,示例代码如下:
<select>
<option th:each="item : ${list}"
th:value="${item.id}"
th:text="${item.name}"
th:selected="${item.id == selectedId}"></option>
</select>
其中,${list}是后端传递的列表数据,${item.id}和${item.name}分别是列表项的id和name属性,${selectedId}是前端传递的选中项的id。
thymeleaf js中获取后端数据
可以使用th:attr来获取后端数据,例如:
<div th:attr="data-id=${id}">这里是获取到的数据</div>
其中,${id}是后端传递过来的数据,可以在js中使用jQuery或者原生js来获取该div的data-id属性值,从而获取到后端数据。