我正在使用 Materializecss 选择表单 http://materializecss.com/forms.html#select并且它需要禁用和选择正确的行为第一个选项。 Thymeleaf 会忽略已禁用的选项,尽管它已被选中。相反,它会选择第一个未禁用的选项。
<div class="input-field col s6">
<select th:field="*{locale}" th:errorclass="invalid">
<option value="" selected="selected" disabled="disabled">Choose your option</option>
<option value="cs">Czech</option>
<option value="en">English</option>
</select>
<label>Locale</label>
</div>
捷克语是自动选择的,但我希望看到选择您的选项被选中。
最佳答案
根据 Enigo 的评论,我通过将 select 元素中的 th:field
替换为 name
和 id
使其正常工作。我还在第一个选项中添加了 th:disabled
和 th:selected
以使其在默认情况下被禁用和选中。
<select name="locale" id="locale" th:errorclass="invalid" required="required">
<option value="" th:disabled="disabled" th:selected="selected">
Choose your option
</option>
<option value="cs">Czech</option>
<option value="en">English</option>
</select>
关于Thymeleaf 不选择禁用选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35655009/