【发布时间】:2017-01-22 21:42:55
【问题描述】:
所以我有一个带有 FORM 元素的表格:
<table id="example" class="sortable">
<caption><h3><strong>Product inventory list</strong></h3></caption>
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Amount</th>
<th>Location</th>
<th>Purchase date</th>
</tr>
</thead>
<tfoot>
<form id="myForm" action="http://wt.ops.few.vu.nl/api/xxxxxxxx" method="get">
<td>
<input type="text" name="name" required>
</td>
<td>
<input type="text" name="category" required>
</td>
<td>
<input type="number" name="amount" required>
</td>
<td>
<input type="text" name="location" required>
</td>
<td>
<input type="date" name="date" required>
</td>
<td>
<input type="submit" value="Submit">
</td>
</form>
</tfoot>
</table>
我填写的信息会发送到我的 API 链接,但现在我需要将我填写的信息直接附加到表格中。
我知道我可以发送 AJAX GET 请求,以获取存储在 API 中的信息,但是如何将返回的 JSON 数据插入到表中?
【问题讨论】:
-
首先,您需要为每个输入添加 id 标签。否则,代码如何知道将 API 返回的值放在哪里?然后谷歌 javascript getElementById
-
@Reisclef
document.querySelector、document.getElementsByClassName等都是向表中添加id属性的替代方法。 -
没错,我的立场是正确的。同意类名,或 querySelector 作为替代方案。但是,可能无法保证 API 顺序按索引符合 GUI。虽然上课可以工作,但似乎格格不入。我不应该将其表述为“需要”,而不是“您可能”。
标签: javascript jquery json ajax api