但是,问题是再次单击添加按钮时,在同一行上创建了列,而不是创建了新行。有人可以帮我解决我的问题吗?
var id = 0;
tblStudent = document.getElementById("tblStudent");
function insertValue() {
id++;
var txtFirstName = document.getElementById("txtFirstName").value;
var txtLastName = document.getElementById("txtLastName").value;
var td = document.createElement("td");
var tdValue = document.createTextNode(id);
if(id != "") {
td.appendChild(tdValue);
tblStudent.insertBefore(td, tblStudent.lastChild);
var td = document.createElement("td");
var tdValue = document.createTextNode(txtFirstName);
if(txtFirstName != "") {
td.appendChild(tdValue);
tblStudent.insertBefore(td, tblStudent.lastChild);
var td = document.createElement("td");
var tdValue = document.createTextNode(txtLastName);
if(txtLastName != "") {
td.appendChild(tdValue);
tblStudent.insertBefore(td, tblStudent.lastChild);
}
}
}
}
<input type="text" id="txtFirstName"><br><br>
<input type="text" id="txtLastName"><br><br>
<button onclick= "insertValue()">Add</button>
<table border = "1px">
<th>ID</th>
<th>FirstName</th>
<th>LastName</th>
<tbody id = "tblStudent">
</tbody>
</table>