select的option数据来自于DB,记住后台返回的数据类型是List<String>
如果用js也能实现,就是先初始化,再设置默认选项。但是这种方式比较麻烦,效率较低。
以下是使用thymeleaf语法标签实现的例子,特别简便 。
<select class="form-select form-select-sm" name="type" id="type">
<option value="null"></option>
<option th:each="atype:${types}" th:value="${atype}" th:text="${atype}" th:selected="${type==atype}"></option>
</select>