Thymeleaf: 我需要单选按钮来增加 th:field 数组列表

huangapple go评论92阅读模式
英文:

Thymleaf: I need the radio buttons to increment the th:field array list

问题

Sure, here is the translated code portion:

<tr th:each="i : ${output.qaCombo}">
    <td th:text="${i.questionPhrase}"></td>
    <input type="radio" th:field="*{answersChosen}" value="A" /><td th:text="${i.Ans1}"></td>
    <input type="radio" th:field="*{answersChosen}" value="B" /><td th:text="${i.Ans2}"></td>
    <input type="radio" th:field="*{answersChosen}" value="C" /><td th:text="${i.Ans3}"></td>
    <input type="radio" th:field="*{answersChosen}" value="D" /><td th:text="${i.Ans4}"></td>
    <br/>
</tr>

Please note that I've only translated the code portion and removed the additional content.

英文:
&lt;tr th:each=&quot;i : ${output.qaCombo}&quot;&gt;
&lt;td th:text=&quot;${i.questionPhrase}&quot;&gt;&lt;/td&gt;
&lt;input type=&quot;radio&quot; th:field=&quot;*{answersChosen}&quot; value=&quot;A&quot; /&gt;&lt;td th:text=&quot;${i.Ans1}&quot;&gt;&lt;/td&gt;
&lt;input type=&quot;radio&quot; th:field=&quot;*{answersChosen}&quot; value=&quot;B&quot; /&gt;&lt;td th:text=&quot;${i.Ans2}&quot;&gt;&lt;/td&gt;
&lt;input type=&quot;radio&quot; th:field=&quot;*{answersChosen}&quot; value=&quot;C&quot; /&gt;&lt;td th:text=&quot;${i.Ans3}&quot;&gt;&lt;/td&gt;
&lt;input type=&quot;radio&quot; th:field=&quot;*{answersChosen}&quot; value=&quot;D&quot; /&gt;&lt;td th:text=&quot;${i.Ans4}&quot;&gt;&lt;/td&gt;
&lt;br/&gt;
&lt;/tr&gt;

Result of my code:

Thymeleaf: 我需要单选按钮来增加 th:field 数组列表

"answersChosen" is an ArrayList<String> in my java code. The webpage only lets me select one radio button because the index of the array is not specified. I need each radio button to fill a spot in the array with the value. Like this: answerChosen[0] = "A", answerChosen1 = "B"
I appreciate any help you guys can offer! Thanks!

答案1

得分: 1

使用Thymeleaf中的th:each,您可以声明一个迭代状态变量。

<tr th:each="i, iter : ${output.qaCombo}">

然后在循环中,您可以使用iter.index或iter.size来执行您需要的操作:

th:field="*{answersChosen[__${iter.index}__]}"

查看教程:使用Thymeleaf - 6.2 保持迭代状态

英文:

With th:each in Thymeleaf you can declare an iteration status variable.

&lt;tr th:each=&quot;i, iter : ${output.qaCombo}&quot;&gt;

and then in the loop you can use iter.index or iter.size to do what you need:

th:field=&quot;*{answersChosen[__${iter.index}__]}&quot;

See Tutorial: Using Thymeleaf - 6.2 Keeping iteration status.

huangapple
  • 本文由 发表于 2020年8月7日 03:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63290522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定