[英]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.