javascript - 如何动态设置 HTML带有百里香的名字
问题描述
我有一个从控制器接收二维数组的表单,并且我已经通过 thymeleaf 生成了表格。
<div class="col-4 offset-4">
<form id="sudokuBoard" method="post" enctype="application/x-www-form-urlencoded" class="form-group text-center">
<div class="container">
<table class="row-eq-height table-striped table-bordered text-center">
<!--/*@thymesVar id="board" type=""*/-->
<tr th:each="row: ${board}">
<td th:each="value: ${row}">
<div th:switch="${value}" class="col-xs-4">
<input name="cellValue" th:case="0" class="content form-control input" style="text-align:center" type="text" pattern="[0-9]*" maxlength="1" value="0" onkeypress="return isNumber(event)" oninput="moveMade()">
<input name="cellValue" th:case="*" class="content form-control input" style="text-align:center;background-color:lightgreen" type="text" th:value="${value}" readonly>
</div>
</td>
</tr>
</table>
<div class="gap-10"></div>
<button type="submit" class="btn btn-outline-primary btn-sm">Check Solution</button>
<button class="btn btn-outline-primary btn-sm" id="btnNewGame">New Game</button>
<button class="btn btn-outline-primary btn-sm" id="solveBoard"> Solve Puzzle</button>
</div>
</form>
</div>
所有输入字段都具有相同的名称,我想通过 Thymeleaf 动态分配名称cellValueX
,以便区分每个单元格以检查用户输入的移动的有效性。
解决方案
你使用属性th:name
。有关可以与 Thymeleaf 一起使用的属性列表,请参阅此内容。我从您的代码中具体不知道您想X
在 in中使用什么cellValueX
,但可能是这样的?
<tr th:each="row, y: ${board}">
<td th:each="value, x: ${row}">
<input th:name="|cellValue${x.index}_${y.index}|" ... />
</td>
</tr>
推荐阅读
- reactjs - 使用反应钩子在上下文提供者中添加多个状态
- haskell - 如何防止某些值(或状态)在线程之间共享?
- yarnpkg - 如何创建一个底层链?
- python - Python 类和 __init___ 构造函数,如何创建一个列表变量?
- flutter - 在使用提供程序包的消费者中,如何将图像设置为 Flutter 中的每个构建?
- python - 每 10 行拆分/划分数据帧
- javascript - 在 Javascript 中创建计数器数组(反应原生)
- node.js - 你能“Ping”一个 Roblox 服务器吗?
- php - PHP设置以':'开头的自定义标题
- html - 文本字母中的背景图像 -> 如果我的屏幕宽度 > 1675px,则在 Mozilla 上不起作用