gpt4 book ai didi

thymeleaf - 没有预选的 Thymeleaf 下拉列表

转载 作者:行者123 更新时间:2023-12-04 17:59:59 26 4
gpt4 key购买 nike

我的 Thymeleaf 页面中有几个下拉列表,如下所示:

 <select name="institution.serviceDept" th:field="*{serviceDept}">
<option th:each="choice : ${serviceDeptList}"
th:value="${choice.serviceDeptId}"
th:attr="choiceinstitutionId=${choice.serviceDeptId}, institutioninstitutionId=*{serviceDept.serviceDeptId},
showselected=(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})"
th:selected="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})"
th:readonly="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})"
th:text="${choice.name}"></option>
</select>

当我查看页面时,列表中的第一个值显示为已选中,并且实际上已作为选中值提交,即使它不是手动选中的。我宁愿默认不选择任何东西,除非它已经被选中。我删除了“th:selected”,这没有任何区别。

谁能告诉我如何不选择任何内容或使用默认值(例如“请选择”)?

最佳答案

在这里张贴我的答案,因为其他人对我不起作用(但你的里程我会有所不同)。显然,您不能在列表中使用 null 或虚拟对象,否则会抛出各种异常。您可以做的是添加另一个空白 <option></option>像这样:

<select name="institution.serviceDept" th:field="*{serviceDept}">
<option th:value="0" text="Please Select"></option>
<option th:each="choice : ${serviceDeptList}"
th:value="${choice.serviceDeptId}"
th:attr="choiceinstitutionId=${choice.serviceDeptId}, institutioninstitutionId=*{serviceDept.serviceDeptId},
showselected=(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})"
th:selected="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})"
th:readonly="(${choice.serviceDeptId} == *{serviceDept.serviceDeptId})"
th:text="${choice.name}"></option>
</select>

注意缺少th在虚拟选项标签上,和一个 th:value

关于thymeleaf - 没有预选的 Thymeleaf 下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36682866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com