英文:
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.
英文:
<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>
Result of my code:
"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.
<tr th:each="i, iter : ${output.qaCombo}">
and then in the loop you can use iter.index or iter.size to do what you need:
th:field="*{answersChosen[__${iter.index}__]}"
See Tutorial: Using Thymeleaf - 6.2 Keeping iteration status.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论