当我通过表单添加新行时,我试图在每个表格行之前添加数字,因此所有添加的行都已编号。这是我用于添加行的代码,它有效。有人知道如何<td>在表格中添加新的吗?
var btn = document.getElementById('btn');
btn.onclick = myFunction;
function myFunction() {
validateForm()
addRow()
}
function addRow() {
//Get data from filled in form
var form = document.getElementById('form');
var newData = [];
for(var i = 0; i < form.length; i++) {
newData[i] = form.elements[i].value;
}
if(validateForm() == true) {
//Put data in table
var table = document.getElementById('table');
var newRow = table.insertRow();
for(var i = 0; i < 3; i++) {
var newCell = newRow.insertCell(i);
newCell.innerHTML = newData[i];
}
}
form.reset();
}
function validateForm() {
var f = document.getElementById('form');
if(f.fname.value == '') {
alert('Please fill in first name');
return false;
}
if(f.lname.value == '') {
alert('Please fill in last name');
return false;
}
if(f.points.value == '') {
alert('Please fill in points');
return false;
}
if(isNaN(f.points.value)) {
alert('Points should be a number')
return false
}
return true;
}
智慧大石
萧十郎
相关分类