【发布时间】:2015-06-25 08:07:49
【问题描述】:
我创建了一个包含大量相似区域的表单。可以使用 jQuery 添加这些区域。每个区域相当于一个用于组装产品的工作步骤,因此它具有相同的输入字段(描述、持续时间、...)。下面是代码(简化为两个硬编码区域):
<html>
<head>
</head>
<body>
<form action="insertNewProduct.php" method="post">
<fieldset id="product-fieldset">
<legend>Produkt</legend>
<div id="step-container">
<input type="hidden" name="step[]" />
<div id="step-table" class="product-table">
<table id="basis-table">
<!-- STEP-BASISDATEN -->
<thead id="step-basis">
<!-- Basisdaten Schritt -->
<tr>
<th class="data-title">Arbeitsschritt</th>
</tr>
<tr>
<th>Bezeichnung:</th>
<td><input type="text" name="step[0][stepDescr]"></td>
</tr>
<tr>
<th>Dauer:</th>
<td><input type="text" name="step[0][stepDur]"></td>
</tr>
</thead>
</table>
</div>
<div id="step-table" class="product-table">
<table id="basis-table">
<!-- STEP-BASISDATEN -->
<thead id="step-basis">
<!-- Basisdaten Schritt -->
<tr>
<th class="data-title">Arbeitsschritt</th>
</tr>
<tr>
<th>Bezeichnung:</th>
<td><input type="text" name="step[1][stepDescr]"></td>
</tr>
<tr>
<th>Dauer:</th>
<td><input type="text" name="step[1][stepDur]"></td>
</tr>
</thead>
</table>
</div>
</div>
</fieldset>
<input type="submit" id="sendForm" value="Send"><input type="reset" id="resetForm" value="Reset">
</form>
</body>
</html>
如您所见,我在步骤中添加了数字
name="step[0][stepDescr]"
因此我得到了想要的结果:
Array
(
[step] => Array
(
[0] => Array
(
[stepDescr] =>
[stepDur] =>
)
[1] => Array
(
[stepDescr] =>
[stepDur] =>
)
)
)
如果我不使用这些数字,我的数组当然无法使用:
Array
(
[step] => Array
(
[0] =>
[1] => Array
(
[stepDescr] =>
)
[2] => Array
(
[stepDur] =>
)
[3] => Array
(
[stepDescr] =>
)
[4] => Array
(
[stepDur] =>
)
)
)
如何使用 jQuery 将这些数字(基于步数)添加到我的 HTML 代码 (!) 中?
【问题讨论】:
-
你可以在php中使用
for()或foreach()循环并打印出来。 -
我不想使用 php 重新排列和处理数组。我的目标是提交一个合适的数组。我认为 - 如果您有 100 个甚至更多的工作步骤 - 一旦提交正确更改数组顺序就会变得非常复杂。
-
检查我的答案。也许它会帮助你。