我的代码:
var old = localStorage.getItem('GotoBeginning');
console.log("old is "+ old);
if (old===true) {
console.log("Returning true");
$("#gobeg").prop('checked', true);
return true;
} else {
console.log("Returning false");
$("#gobeg").prop('checked', false);
return false;
}
GotoBeginning 在 localStorage 中的值为 true。 我的 console.log 显示:
old is true
Returning false
我期望得到以下输出:
old is true
Returning true
最佳答案
浏览器中的存储api只存储字符串。这应该有效:
if (old === 'true') {
// ^ ^
} else {
}
正如 IrkenInvader 在评论中提到的,您可以像这样提取正确的类型:
var old = JSON.parse(localStorage.getItem('GotoBeginning'))
// now old will be a proper boolean so this will work
if (old) {
} else {
}
关于Javascript boolean 逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48591297/