gpt4 book ai didi

javascript - 为什么在 “LocalStorage” 中添加/删除元素不起作用?

转载 作者:行者123 更新时间:2023-12-03 01:29:57 25 4
gpt4 key购买 nike

我要添加:localStorage中用户的姓名、电话和邮箱使用方法:addContact()以及 localStorage 中的数据,我使用方法 show() 创建表.

也不会发生删除联系人的情况,我正在尝试使用以下方法:deleteContact(e)

  • 当我添加联系人时,我收到以下错误:Uncaught TypeError: Cannot read property 'value' of undefined
  • 删除时收到以下错误:Uncaught TypeError: Cannot read property 'splice' of undefined

帮我解决这个问题

//Product Creation Class
class LocalStorage {
constructor(name, phone, email) {
this.name = name;
this.phone = phone;
this.email = email;

}
}
// Сlass where products are recorded
class Book {
constructor() {

this.products = [];
this.name = document.getElementById("name");
this.phone = document.getElementById("phone");
this.email = document.getElementById("email");
this.buttAdd = document.getElementById("add");
this.book = document.getElementById("addBook");
}

//method for adding a product
addContact() {
let isNull = this.name.value != '' && this.phone.value != '' && this.email.value != '';
if (isNull) {
let obj = new LocalStorage(this.name.value, this.phone.value, this.email.value);
this.products.push(obj);
localStorage['addbook'] = JSON.stringify(this.products);
this.show();
}
}

//method for remove product by name
deleteContact(e) {
if (e.target.className === "delbutton") {
let remID = e.target.getAttribute('data-id');
this.products.splice(remID, 1);
localStorage['addbook'] = JSON.stringify(this.products);
this.show();
}
}

// method to draw the table with product property (
// name, phone, email)
show() {
if (localStorage['addbook'] === undefined) {
localStorage['addbook'] = '';
} else {
this.products = JSON.parse(localStorage['addbook']);

this.book.innerHTML = '';
for (let e in this.products) {
let table = ` <table id="shop" class="entry">
<tr>
<th>Name:</th>
<th id="filter">Phone:</th>
<th>Email:</th>
<th class="dels"></th>
</tr>
<tbody>
<tr class="data">
<td>${this.products[e].name}</td>
<td>${this.products[e].phone}</td>
<td>${this.products[e].email}</td>
<td class="del"><a href="#" class="delbutton" data-id="' + e + '">Delete</a></td>
</tr>
</tbody>
</table>`;
this.book.innerHTML += table;
}
}

}

OperationsWithContacts() {
// add new product by click
this.buttAdd.addEventListener('click', this.addContact);
// delete product by name after click
this.book.addEventListener('click', this.deleteContact);
console.log(this.products);
}
}

let shop = new Book();
shop.show();
shop.OperationsWithContacts();
<div class="Shop">
<div class="add-product">
<h1>Add product</h1>
<form name="addForm">
<label for="name" >Name of product</label>
<input type="text" id="name" class="input-product">
<label for="phone">Price of product</label>
<input type="number" id="phone" class="input-product">
<label for="email">Count of product</label>
<input type="text" id="email" class="input-product">
<button id="add" type="button">Add</button>
</form>
</div>
<div class="product-table">
<h2>Address book</h2>
<div id="delete-form">
<label for="name-delete">Search product by name</label>
<input type="text" id="name-delete" class="input-delete">
</div>
<div id="addBook"></div>
</div>
</div>

enter code here

最佳答案

我看到的明显问题是,您的函数 addContact()deleteContact() 中的 this 代表一个 button-HTML-Element 而不是您认为的类。

只需稍微更改一下代码,您就会看到://添加产品的方法

addContact() {
console.log('addContact()', this);

let isNull = this.name.value != '' && this.phone.value != '' && this.email.value != '';
if (isNull) {
let obj = new LocalStorage(this.name.value, this.phone.value, this.email.value);
this.products.push(obj);
localStorage['addbook'] = JSON.stringify(this.products);
this.show();
}
}

所以你可能想改变你的绑定(bind):

document.querySelector('#add').addEventListener('click', this.addContact);

document.querySelector('#add').addEventListener('click', this.addContact.bind(this));

正确重用this快捷方式。

编辑:

//Product Creation Class
//REM: Not the best name choice here.. localStorage <> LocalStorage
class LocalStorage{
constructor(name, phone, email){
this.name = name;
this.phone = phone;
this.email = email
}
};

//Сlass where products are recorded
class Book{
constructor(){
this.products = [];
this.name = document.getElementById("name");
this.phone = document.getElementById("phone");
this.email = document.getElementById("email");
this.buttAdd = document.getElementById("add");
this.book = document.getElementById("addBook")
};

//method for adding a product
addContact(){
let isNull = this.name.value != '' && this.phone.value != '' && this.email.value != '';
if(isNull){
let obj = new LocalStorage(this.name.value, this.phone.value, this.email.value);
this.products.push(obj);
localStorage['addbook'] = JSON.stringify(this.products);
this.show();
}
};

//method for remove product by name
deleteContact(e) {
if(e.target.className === "delbutton"){
let remID = e.target.getAttribute('data-id');
this.products.splice(remID, 1);
localStorage['addbook'] = JSON.stringify(this.products);
this.show();
}
};

//method to draw the table with product property (
//name, phone, email)
show(){
if(localStorage['addbook'] === undefined) {
//REM: An empty string is no valid JSON to be serialised
localStorage['addbook'] = '[]'
}
else{
this.products = JSON.parse(localStorage['addbook']);
this.book.innerHTML = '';

for(let e in this.products){
let table = ` <table id="shop" class="entry">
<tr>
<th>Name:</th>
<th id="filter">Phone:</th>
<th>Email:</th>
<th class="dels"></th>
</tr>
<tbody>
<tr class="data">
<td>${this.products[e].name}</td>
<td>${this.products[e].phone}</td>
<td>${this.products[e].email}</td>
<td class="del"><a href="#" class="delbutton" data-id="' + e + '">Delete</a></td>
</tr>
</tbody>
</table>`;

this.book.innerHTML += table;
}
}
};

OperationsWithContacts(){
// add new product by click
this.buttAdd.addEventListener('click', this.addContact.bind(this));

// delete product by name after click
this.book.addEventListener('click', this.deleteContact.bind(this));
console.log(this.products);
}
};

;window.onload = function(){
let shop = new Book();
shop.show();
shop.OperationsWithContacts()
};
<div class="Shop">
<div class="add-product">
<h1>Add product</h1>
<form name="addForm">
<label for="name" >Name of product</label>
<input type="text" id="name" class="input-product">
<label for="phone">Price of product</label>
<input type="number" id="phone" class="input-product">
<label for="email">Count of product</label>
<input type="text" id="email" class="input-product">
<button id="add" type="button">Add</button>
</form>
</div>
<div class="product-table">
<h2>Address book</h2>
<div id="delete-form">
<label for="name-delete">Search product by name</label>
<input type="text" id="name-delete" class="input-delete">
</div>
<div id="addBook"></div>
</div>
</div>

关于javascript - 为什么在 “LocalStorage” 中添加/删除元素不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51347855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com