【问题标题】:Uncaught TypeError: Object [object Object] has no method 'tableRow' [duplicate]未捕获的类型错误:对象 [object Object] 没有方法“tableRow”[重复]
【发布时间】:2017-04-05 21:03:48
【问题描述】:

我正在创建通讯录并希望在用户输入数据时更新表格,但我不断收到有关“tableRow”的错误消息。我尝试将其更改为不同的函数名称等,但似乎找不到解决方案。

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;
};

Contact.prototype.tableRow = function(){
var tr = "<tr><td>" + this.name + "</td><td>" +
         this.address + "</td><td>" + this.email +
         "</td><td>" + this.address + ">";
};


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;
};

【问题讨论】:

  • 如果您想提供更多信息,请edit your original question
  • 您需要添加一个Contact.prototype.tableRow=function(){} 并使其返回一个tr html
  • .tableRow() 方法定义在哪里?
  • 非常感谢@ArunPJohny

标签: javascript html html-table


【解决方案1】:

给Contact添加一个tableRow函数

Contact.prototype.tableRow = function() {
    return '<tr>' + '<td>' + this.name + '</td>' + '<td>' + this.address + '</td>' + '<td>' + this.email + '</td>' + '<td>' + this.postcode + '</td>' + '</tr>';
}

【讨论】:

    猜你喜欢
    • 2014-01-08
    • 2013-04-19
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多