gpt4 book ai didi

javascript - 刷新页面并插入新值后,本地存储值会重置

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

因此,我成功地将想要的值保存到本地存储,但每次我点击刷新并插入新数组时,这些值都会被清除,只保留新数组。如何才能永久保存所有值?谢谢!

let users = []
let logged = false;
let indexUtilizador = 0;

window.onload = function () {

class User {
constructor(user, pass, email, tipouser,lastId) {
this.user = user
this.pass = pass
this.email = email
this.tipouser = tipouser ou docente
this._id = User.getLastId() + 1
}
}


let registerUser = document.getElementById('newAccount')

document.getElementById("register").onclick = function() {
console.log("Ola")
let user = document.getElementById('user').value
let pass = document.getElementById('pass').value
let email = document.getElementById('email').value
let usertype = document.getElementById('usertype').value

let newUser = new User(user, pass, email, usertype)

users.push(newUser)

localStorage.setItem("users", JSON.stringify(users))

}

}

最佳答案

使用类似的东西来插入数据。

localStorage.setItem("users_"+new Date().getTime(), JSON.stringify(users))

这会将用户保存为不同的记录。

关于javascript - 刷新页面并插入新值后,本地存储值会重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50893279/

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