更新javascript表:未捕获类型错误:对象[Object Object]没有方法';tableRow';

更新javascript表:未捕获类型错误:对象[Object Object]没有方法';tableRow';,javascript,html,web-applications,html-table,Javascript,Html,Web Applications,Html Table,我正在制作一个联系人应用程序,该应用程序使用用户输入更新一个表,但似乎无法在输入数据后更新该表。我只收到上面的错误。我不知道该如何改变这个方法,我尝试了很多不同的函数,但是运气不好 var nameField, addressField, emailField, postcodeField; function twoDigit(v){ if(v.toString().length < 2){ return "0"+v.toString(); } else { return

我正在制作一个联系人应用程序,该应用程序使用用户输入更新一个表,但似乎无法在输入数据后更新该表。我只收到上面的错误。我不知道该如何改变这个方法,我尝试了很多不同的函数,但是运气不好

var nameField, addressField, emailField, postcodeField;

function twoDigit(v){
if(v.toString().length < 2){
    return "0"+v.toString();
} else {
    return v.toString();
}
}

var Contact = function(name, address, email, postcode){
this.name = name;
this.address = address;
this.email = email;
this.postcode = postcode;
this.completed = false;
};

var contacts = [];

Contact.prototype.toString = function(){
var s = this.name + '\n' + this.address + '\n' + this.email + + '/n'
  +  this.postcode.toString() + '\n';
if(this.completed){
    s += "Completed\n\n";
} else {
    s += "Not Completed\n\n";
}
return s;
};

var addContact = function(nameField, addressField, emailField, postcodeField){
a = new Contact(nameField.value, addressField.value, 
emailField.value, postcodeField.value);
contacts.push(a);
};

var clearUI = function(){
var white = "#fff";
nameField.value = "";
nameField.style.backgroundColor = white;
addressField.value = "";
addressField.style.backgroundColor = white;
emailField.value = "";
emailField.style.backgroundColor = white;
postcodeField.value = "";
postcodeField.style.backgroundColor = white;

};

var updateList = function(){
var tableDiv = document.getElementById("table"),
    table = "<table border='1'><thead><th>Name</th>
<th>Address</th><th>Email</th><th>Postcode</th><th>Completed</th></thead>";

for(var i=0, j=contacts.length; i<j; i++){
    var contacts1 = contacts[i];
     table += contacts1.tableRow();
}
table+="</table>";
tableDiv.innerHTML = table;
};

var add = function(){
addContact(nameField, addressField, emailField, postcodeField);
clearUI();
updateList();
};

var cancel = function(){
clearUI();
updateList();
};

window.onload = function(){
nameField = document.getElementById("name");
addressField = document.getElementById("address");
emailField = document.getElementById("email");
postcodeField = document.getElementById("postcode");
okButton = document.getElementById("ok");
okButton.onclick = add;
cancelButton = document.getElementById("cancel");
cancelButton.onclick = cancel;
clearUI();
}; 

var showTable = function(){
var tableDiv = document.getElementById("table"),
    table = "<table border='1'><thead><th>Name</th>
<th>Address</th><th>Email</th>  <th>Postcode</th></thead>";

for(var i=0, j=contacts.length; i<j; i++){
    var contacts1 = contacts[i];
    table += contacts1.tableRow();
}
table+="</table>";
tableDiv.innerHTML = table;
};
var名称字段、地址字段、电子邮件字段、邮政编码字段;
函数两位数(v){
如果(v.toString().长度<2){
返回“0”+v.toString();
}否则{
return v.toString();
}
}
var联系人=功能(姓名、地址、电子邮件、邮政编码){
this.name=名称;
this.address=地址;
this.email=电子邮件;
this.postcode=邮政编码;
this.completed=false;
};
var触点=[];
Contact.prototype.toString=函数(){
var s=this.name++'\n'+this.address++'\n'+this.email++'/n'
+this.postcode.toString()+'\n';
如果(本文件已完成){
s+=“已完成\n\n”;
}否则{
s+=“未完成\n\n”;
}
返回s;
};
var addContact=函数(名称字段、地址字段、电子邮件字段、邮政编码字段){
a=新联系人(nameField.value、addressField.value、,
emailField.value,postcodeField.value);
触点。按下(a);
};
var clearUI=函数(){
var white=“#fff”;
nameField.value=“”;
nameField.style.backgroundColor=白色;
addressField.value=“”;
addressField.style.backgroundColor=白色;
emailField.value=“”;
emailField.style.backgroundColor=白色;
postcodeField.value=“”;
postcodeField.style.backgroundColor=白色;
};
var updateList=函数(){
var tableDiv=document.getElementById(“表”),
table=“名称
AddressEmailPostcodeCompleted”;

对于(var i=0,j=contacts.length;iCode不完整:contacts数组是什么?这段代码看起来像是复制粘贴出错了……前两条语句和后两条语句看起来像是要通过字符串连接来构建表,但中间的语句表明要通过dom添加行。即使tableRow()可能必须更改为insertRow。即使这样,也假设contacts是对表元素的引用。实际上,contacts必须是表元素的数组……此代码示例非常笨拙。