來臨所以我有一個表單元素的表:如何插入一個表中返回的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