thymeleaf select multiple 反显
要在Thymeleaf中实现select多选框的反显,需要在html中使用Thymeleaf表达式来设置selected属性。
首先,确定select元素的name和th:field属性值与模型中的属性值对应。例如,如果模型中有一个名为"selectedOptions"的List属性,那么可以将select元素的name和th:field设置为"selectedOptions"。
接下来,使用Thymeleaf的each指令遍历可选项列表,并在option标签中使用Thymeleaf的if指令来检查每个可选项是否在selectedOptions列表中。如果是,则设置selected属性为"selected"。
以下是一个示例代码:
```html
<select name="selectedOptions" multiple>
  <option th:each="option : ${optionsList}"
          th:value="${option}"
          th:text="${option}"
          th:selected="${selectedOptions != null ains(option)}">
  </option>thymeleaf用法
</select>
```
上述代码首先遍历optionsList列表,并为每个可选项设置值和显示文本。然后,使用Thymeleaf的selected指令来检查selectedOptions列表中是否包含当前可选项。如果包含,则设置selected属性为"selected"。
确保在传递模型到Thymeleaf视图之前,将selectedOptions属性设置为选中的选项列表。
希望这可以帮助到您。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。