我正在制作一个需要显示和保存用户输入的页面
我有这些变量
var hotdogs = 0;
var bread = 0;
var hotdog = 0;
var sauce = 0;
var bakeries = 0;
var butchers = 0;
var sauceries = 0;
我已经尝试用这个创建一个保存按钮,虽然加载工作,我无法在非常基本的数学中使用它们。然后,在数学问题之后的这些数字也显示在页面上。
当我使用此代码时,数字会加载,但是当我将数字加1时,它会在结尾处添加1而不会增加1。
if(typeof(Storage)!=="undefined") {
var hotdogs = localStorage.getItem("hotdogs");
var bread = localStorage.getItem("bread");
var hotdog = localStorage.getItem("hotdog");
var sauce = localStorage.getItem("sauce");
var bakeries = localStorage.getItem("bakeries");
var butchers = localStorage.getItem("butchers");
var sauceries = localStorage.getItem("sauceries");
}
else {
var hotdogs = 0;
var bread = 0;
var hotdog = 0;
var sauce = 0;
var bakeries = 0;
var butchers = 0;
var sauceries = 0;
}
var auto = 1;
var automake = 0;
var one = 1;
function save () {
localStorage.setItem("bread", bread);
localStorage.setItem("hotdog", hotdog);
localStorage.setItem("sauce", sauce);
localStorage.setItem("hotdogs", hotdogs);
localStorage.setItem("bakeries", bakeries);
localStorage.setItem("butchers", butchers);
localStorage.setItem("sauceries", sauceries);
}
答案 0 :(得分:0)
存储项始终存储为字符串。这就是为什么在存储它们之前必须JSON.stringify
个对象的原因,例如。
出于这个原因,+
表示连接,而不是添加。如果要检索整数,则必须parseInt(item, 10)
。