gpt4 book ai didi

javascript - localStorage 刷新后清空

转载 作者:行者123 更新时间:2023-11-29 15:13:19 25 4
gpt4 key购买 nike

出于某种原因,每次我尝试在 Reservas 属性下将内容保存到我的 localStorage 时,如果我刷新页面,localStorage 中的数据将被清除。

我已经使用类似的方法将注册表单中的数据存储在我的应用程序(其他页面)上,并且它存储得很好。我不知道为什么这不起作用。

$(document).ready(function() {
var userlog;
var reserva = {}
var reservas = []
var dia_semana_input = document.getElementById('sel_dia')
var refeicao_input = document.getElementById('sel_tipo')
var prato_input = document.getElementById('sel_prato')

var error_fix = {
nome: "1",
dia: "1",
refeicao: "1",
prato: "1"
}
reservas.push(error_fix)
localStorage.setItem("Reservas", JSON.stringify(reservas));
var utilizadores = JSON.parse(localStorage.getItem('utilizadores'))
console.log(utilizadores)
console.log(reservas)
reservas = JSON.parse(localStorage.getItem('Reservas'))
/* verificação para mais tarde utilizar nas reservas*/
for (var i = 0; i < utilizadores.length; i++) {
if (utilizadores[i].ativo == 1) {
userlog = utilizadores[i].Username
}
}

$("#btnReservar").click(function() {
/*Stores object into array */
/*which then stores into local storage(?)*/
var dia_semana = dia_semana_input.value
var refeicao = refeicao_input.value
var prato = prato_input.value
for (var i = 0; i < reservas.length; i++) {
if (reservas[i].nome == userlog && reservas[i].dia == dia_semana) {
console.log("primeiro")
alert('Já existe uma reserva para esse dia!')
} else {
console.log("segundo")
reserva.nome = userlog
reserva.dia = dia_semana
reserva.refeicao = refeicao
reserva.prato = prato
reservas.push(reserva)
console.log(reservas)
}
}

localStorage.setItem('Reservas', JSON.stringify(reservas))
});
})

最佳答案

您可以使用该键调用 setItem。这将导致它被设置为 reservas 中的值。也许您想调用 getItem

关于javascript - localStorage 刷新后清空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52192215/

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