来临所以我有一个表单元素的表:如何插入一个表中返回的JSON数据,从API
<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返回值的位置?然后,google javascript getElementById – Reisclef
@Reisclef“document.querySelector”,“document.getElementsByClassName”等都是向表中添加“id”属性的替代方法。 – djthoms
没错,我纠正了。同意将类名或querySelector作为替代方法。但是,可能无法保证API顺序能够通过索引符合GUI。虽然课堂可以工作,但似乎不合适。我不应该把它写成“需要”,而不是“你可能”。 – Reisclef