我正在尝试从list phases创建一个表,其中每一行都有与该行的列表项phase相关的表单--因此第1行有一个单元格表示“相位1",然后有一个按钮将重命名为”阶段1“,然后下一行有”第2阶段“,并为此设置了一个按钮。
但是,当我试图从表单中访问列表迭代的索引时,它总是返回0,所以第2行的第一个单元格仍然是“阶段2”,但是表单仍然将“阶段1”的索引传递给HTTP请求。它仍然可以看到列表,因为如果我将其更改为返回phaseStat.size,而不是phaseStat.index,它就会给出正确的数字。
包含phase名称的单元格工作得很好,只有在我进入<form>标记之后,它才会卡在列表的第一项上。如何使表单与表的其他部分的迭代保持同步?
<table class="table">
<tr th:each="phase : ${phases}">
<td class="table-light" th:text="${phase}"></td>
<td>
<form th:action="@{/tasks/phases/rename}">
<button type="button" class="btn btn-outline-primary btn-sm" title="Rename" onclick="openForm('renamephaseform')">Rename</button>
<div class="form-popup card mb-3" id="renamephaseform">
<h3 class="card-header">Rename
<button type="button" class="btn btn-outline-secondary btn-sm closebutton" title="Close menu" onclick="closeForm('renamephaseform')">⛌</button>
<br>
</h3>
<div class="card-body">
<input type="text" class="form-control" id="phasename" placeholder="Phase name" th:name="newname">
<button type="submit" class="btn btn-primary" title="Rename" th:name="phasenumber" th:value="${phaseStat.index}">Rename <span th:text="${phase}"></span></button>
</div>
</div>
</form>
</td>
</tr>
</table>
<script>
function openForm(name) {
document.getElementById(name).style.display = "block";
}
function closeForm(name) {
document.getElementById(name).style.display = "none";
}
</script>编辑:奇怪的是,在这个例子中,迭代非常好,我看不出有什么区别.
<table class="table">
<tr th:each="album : ${albums}">
<td class="table-light" th:text="${album.name} + ' (' + ${album.artist} + ')'"></td>
<td>
<form th:action="@{/index}">
<button type="submit" class="btn btn-primary btn-sm" th:name="id" th:value="${album.id}">Open album</button>
</form>
</td>
</tr></table>发布于 2022-09-01 14:36:34
因此,我最后所做的工作(多亏了andrewJames)是在表单id中包含迭代索引,以创建唯一的名称。
因此,对于打开按钮,我包括了th:attr="onclick='openForm(\'renamephaseform' + ${phaseStat.index} + '\')'"、关闭按钮th:attr="onclick='closeForm(\'renamephaseform' + ${phaseStat.index} + '\')'"和表单标记th:attr="id='renamephaseform' + ${phaseStat.index}"。
Elswhere,我有时会使用字符串值(例如阶段名称、任务名称等)来使检查器中的检查更容易。
https://stackoverflow.com/questions/73473467
复制相似问题