使用Thymeleaf选择已禁用的默认选项

时间:2016-10-03 08:33:11

标签: thymeleaf

我有一个我用Thymeleaf创建的精选菜单。我需要菜单没有默认选择。通常这是通过禁用的额外选项完成的,我也是这样做的。现在,问题是我无法将选定的属性渲染到生成的HTML中。这似乎打破了IE,然后默认为第一个非禁用选项。这就是我所拥有的:

    <select th:field="*{serviceName}" required="required" >
      <option th:selected="true" th:disabled="true" th:value="NOT_SELECTED" th:text="'Pick one'"></option>
      <option th:each="entry : ${form.services}"
              th:value="${entry.key}" th:text="${entry.value}">
      </option>
    </select>

它呈现如下:

    <select required="required" id="serviceTechnicalName" name="serviceTechnicalName">
      <option disabled="disabled" value="NOT_SELECTED">Pick one</option>
      <option value="SERVICE1">Service One</option>
      <option value="SERVICE2">Service Two</option>
    </select>

我做错了什么?我已经至少花了一个小时摆弄这些不同选项的不同组合,它不应该是这么难......

FWIW,this似乎是一个重复的问题,但那里的答案并没有为我做。那里也没有接受的答案。

2 个答案:

答案 0 :(得分:0)

显然,当使用th:字段时它不起作用。看看this post。 但是,我不认为你必须使用th:选择,因为服务器中没有处理。 你尝试过类似的东西吗?

<select th:field="*{serviceName}" required="required" >
      <option value="" selected="selected">Selecione</option>
      <option th:each="entry : ${form.services}"
              th:value="${entry.key}" th:text="${entry.value}">
      </option>
</select>

答案 1 :(得分:0)

嗯,forum post Kimy82 suggested深入到Java配置漏洞,因此该解决方案对我没有吸引力。

然后我将Spring Boot从1.3.3-RELEASE升级到1.4.0-RELEASE。然后我花了一些时间尝试将Thymeleaf从版本2升级到version 3,但显然无法获得所有依赖项并且排除了传递依赖项。

所以最后我做到了这一点:

<script type="text/javascript">
window.addEventListener('load', function () {
    document.getElementById('serviceTechnicalName').value='NOT_SELECTED';
});
</script>

现在,这不是我想要的答案,但它确实起作用了......

相关问题