我無法讓代碼工作。有人可以指出我做錯了什麼嗎?我只想使用JavaScript將輸入打印到表格中。添加html輸入到表
HTML
Item:
<input type="text" name="item" id="item" /><br />
Quantity:
<input type="text" name="quantity" id="quantity" /><br />
Price: AUD
<input type="text" name="price" id="price" /><br /><br />
<input type="button" value="Add Product +" onClick="addRow()" id="add"><br /><br />
<table id="table" border="1">
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</table>
的JavaScript
function addRow() {
"use strict";
var table = document.getElementById("table");
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var td3 = document.createElement("td");
td1.innerHTML = document.getElementById("item").value;
td2.innerHTML = document.getElementById("quantity").value;
td3.innerHTML = document.getElementById("price").value;
row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
table.children[0].appendChild(row);
});
哪裏變量'row'從何而來?您的Java腳本控制檯(例如Firebug)會在執行代碼時顯示這是一個錯誤。 – reto
您沒有名爲'row'的對象。 – Carlangueitor
什麼是''使用嚴格的「;'和你的函數在哪裏關閉'}'括號。 – Roopendra