thymeleaf 下拉列表及默认选中
时间: 2023-10-26 21:21:55 浏览: 157
在 Thymeleaf 中实现下拉列表及默认选中可以采用以下步骤:
1. 在 HTML 中使用 Thymeleaf 标签创建下拉列表。
```html
<select th:field="*{fruit}">
<option value="">请选择水果</option>
<option value="apple">苹果</option>
<option value="banana">香蕉</option>
<option value="orange">橙子</option>
</select>
```
其中,`th:field` 属性绑定了表单中的字段,`*{}` 表示表单对象,`fruit` 表示字段名称。
2. 设置默认选中项。
```html
<select th:field="*{fruit}">
<option value="">请选择水果</option>
<option value="apple" th:selected="${fruit=='apple'}">苹果</option>
<option value="banana" th:selected="${fruit=='banana'}">香蕉</option>
<option value="orange" th:selected="${fruit=='orange'}">橙子</option>
</select>
```
使用 Thymeleaf 的 `th:selected` 属性可以根据表单对象的值来设置选中项。`${fruit=='apple'}` 表示如果表单对象 `fruit` 的值为 `'apple'`,则选中该项。
这样就可以在 Thymeleaf 中实现下拉列表及默认选中了。
阅读全文
相关推荐















