$(document).ready(init);
function init()
{
var contacts = localStorage.contacts ? JSON.parse(localStorage.contacts) : [];
var contact;
// if there is no storage yet, on click of button new person gets in.
$("#submit").on("click", addNewContact);
}
function addNewContact()
{
// push every person in empty array by forming an object.
contact = {
name: $("#name").val,
email: $("#email").val,
phone: $("#phone").val,
address: $("#address").val
}
contacts = JSON.stringify(contact);
localStorage.contacts = contacts;
updatePerson();
}
function updatePerson(){
var $tr = $("<tr>")
.append($("<td>").text(contact.name))
.append($("<td>").text(contact.email))
.append($("<td>").text(contact.phone))
.append($("<td>").text(contact.address));
$("#table").append($tr);
}
这段代码给了我错误:
未捕获TypeError:this.each不是函数:Uncaught TypeError: this.each不是functionm.fn.extend.val @ jquery-1.11.3.min.js:5m.access @ jquery-1.11.3.min.js:4m.fn.extend.text @ jquery-1.11.3.min.js:4updatePerson @ Contacts.js:26addNewContact @ Contacts.js:22m.event.dispatch @jquery-1.11.3.min.js:4r.handle @ jquery-1.11.3.min.js:4导航到文件:index.html
答案 0 :(得分:1)
确保已正确分配联系人,因为它在代码中无效,因为.val而不是.val()