没有预选的百里香叶下拉列表 [英] Thyme leaf dropdown list without preselecting

查看:34
本文介绍了没有预选的百里香叶下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have several drop down lists in my Thymeleaf pages that look such as:

 <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",这没有任何区别.

When I view the page, the first value in the list appears selected, and in fact is submitted as the selected value even if its not selected manually. I would prefer to nothing selected by default unless it has already been selected. I removed the " th:selected" and that doesn't make any difference.

谁能告诉我如何不选择任何内容或使用默认值,例如请选择"?

Can anyone tell me how to either have nothing selected or perhaps a default value such as "Please select" ?

推荐答案

在这里发布我的答案,因为其他人对我不起作用(但您的里程因我而异).显然,您不能在列表中使用空值或虚拟对象,否则会引发各种异常.你可以做的是添加另一个空白 像这样:

Posting my answer here, as the others didn't work for me (but your mileage my vary). Apparently you can't use a null, or a dummy object in your list or all sorts of exceptions are thrown. What you can do is adding another blank <option></option> like this:

<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>

注意虚拟选项标签上缺少 thth:value

Note the lack of th on the dummy option tag, and a th:value

这篇关于没有预选的百里香叶下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆