[英]Check if the value of the local storage is equal or not to that of a Php variable
[英]Check if local storage value is equal to
我想知道如何检查存储在本地存储中的项目的值为true或false。
这就是我所拥有的,但是if语句不起作用。
function setCBT(){
localStorage.setItem('testObject', true);
}
function alertLocalStorage(){
var object = localStorage.getItem('testObject');
if(object == true) {
alert("This item is true");
}
else {
alert("This item is false");
}
}
您可以简单地使用
if(object) {
// true
}
为什么不使用JSON.parse()
. JSON.parse()
方法将字符串解析为JSON,可以选择转换解析产生的值。
if(JSON.parse(object)) {
alert("This item is true");
}
注意使用JSON.parse()
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.