【问题标题】:Thymleaf: I need the radio buttons to increment the th:field array listThymeleaf:我需要单选按钮来增加 th:field 数组列表
【发布时间】:2020-08-21 18:07:14
【问题描述】:
<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>

我的代码结果:

"answersChosen" 是我的 java 代码中的一个 ArrayList。该网页只允许我选择一个单选按钮,因为未指定数组的索引。我需要每个单选按钮用值填充数组中的一个点。像这样: answerChosen[0] = "A", answerChosen[1] = "B" 我很感激你们可以提供的任何帮助!谢谢!

【问题讨论】:

    标签: java html thymeleaf


    【解决方案1】:

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

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

    然后在循环中你可以使用 iter.index 或 iter.size 来做你需要的事情:

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

    参见教程:Using Thymeleaf - 6.2 Keeping iteration status

    【讨论】:

      猜你喜欢
      • 2019-10-17
      • 2021-09-25
      • 2018-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2017-11-21
      • 1970-01-01
      • 2019-04-14
      相关资源
      最近更新 更多