spring th:在带有 Thymeleaf 的选择/选项中选择一个数字不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24203220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
th:selected a number in a select/option with Thymeleaf doesn't work
提问by Tudor
I have this code
我有这个代码
<div th:class="form-group">
<td><label class="control-label leftMargin10 rightMargin10" scope="col" th:text="#{insertHours.hhFrom}">Attivita'</label></td>
<td><select class="form-control" th:field="*{hhFrom}">
<option th:each="i : ${#numbers.sequence(0, 23)}" th:value="${i}" th:text="${i}" th:selected="${ i==9 } ">Options</option>
</select>
</td>
</div>
When I try to add a condition in th:selected it doesn't work.
当我尝试在 th:selected 中添加条件时,它不起作用。
I have also replaced with this code:
我也用这个代码替换了:
th:attr="${i==9}? selected=selected: '' "
but the result is the same.
但结果是一样的。
The HTML
HTML
<select class="form-control" id="hhFrom" name="hhFrom">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
</select>
Thanks in advance to answers
提前感谢回答
采纳答案by geoand
回答by iaforek
Another hack that, is pretty simple and works, is to close the select tag: <select ... />
另一个非常简单且有效的hack是关闭 select 标签:<select ... />
Please note />
at the end of first line:
请注意/>
第一行的末尾:
<select th:field="*{percentage}" />
<option th:each="i : ${#numbers.sequence(0, 100)}" th:value="${i}" th:text="${i}" th:selected="${i==75}"></option>
</select>
Renders as:
呈现为:
<select id="percentage" name="percentage" />
<option value="0">0</option>
<option value="1">1</option>
...
<option value="74">74</option>
<option value="75" selected="selected">75</option>
<option value="76">76</option>
...
<option value="100">100</option>
</select>
Both web browser and Thymeleaf will handle this fine.
网络浏览器和 Thymeleaf 都可以很好地处理这个问题。
I used Thymeleaf v.3.0.9.RELEASE
我使用了 Thymeleaf v。3.0.9.RELEASE
回答by Aleksandar Nikolic
Also I found out that if you put <div>
tag around option fields, selected="selected" will also work. e.g
我还发现,如果您<div>
在选项字段周围放置标签, selected="selected" 也将起作用。例如
<select th:field="*{name}">
<div>
<option disabled="true" selected="selected" value="">Select</option>
<option value="1">first</option>
<option value="2">second</option>
</div>
</select>