2016-10-03 135 views
0

我有一個我用Thymeleaf創建的選擇菜單。我需要菜單沒有默認選擇。通常這是通過一個禁用的額外選項完成的,而且我也完成了。現在,問題在於我無法獲得選定的屬性以渲染到生成的HTML中。這似乎破壞IE,然後默認爲第一個非禁用選項。這是我的本錢:選擇與Thymeleaf禁用的默認選項

<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似乎是一個重複的問題,但答案沒有爲我做。那裏也沒有被接受的答案。

回答

0

顯然,使用th:field時它不起作用。看看this post。 但是,我不認爲你必須使用th:selected,因爲服務器中沒有處理。 你有tryied類似:

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

謝謝,但我已經試過(和重試)這種方法,它不起作用。不過,我現在正在閱讀你鏈接的文章。 – ZeroOne

0

好了,那forum postKimy82 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> 

現在,這不是我一直在尋找的答案,但它的工作...