小提琴是here
我在以下结构localStorage.COMMENT_DESCRIPTION.list_no
中添加了值,但它始终显示未定义。
但是当我打印到console.log时,它会显示数据。问题是当我检查条件时它无法正常工作
var comment_description = {};
$("#add_list_img").click(function()
{
if(localStorage.COMMENT_DESCRIPTION)
{
alert(localStorage.COMMENT_DESCRIPTION.list_no); # Here is the problem. I have added the value but the variable is empty
if(localStorage.COMMENT_DESCRIPTION.list_no == undefined)
{
alert("Here so");
cart_val = localStorage.COMMENT_DESCRIPTION.list_no || 1;
comment_description.list_no = cart_val+1;
console.log(comment_description);
storage();
}
else
{
alert("here");
cart_val = comment_description.list_no;
comment_description.list_no = cart_val+1;
storage();
}
}
else
{
cart_val = 1;
comment_description.list_no = cart_val+1;
storage();
}
});
function storage()
{
if(localStorage.COMMENT_DESCRIPTION)
{
var oldItems = JSON.parse(localStorage.getItem('COMMENT_DESCRIPTION'));
console.log("strogae "+comment_description.list_no);
var newItems = $.extend({}, oldItems, comment_description);
localStorage.setItem('COMMENT_DESCRIPTION',JSON.stringify(newItems));
}
else
{
var lc_no = {"list_no":1,"card_no":1};
var newItems = $.extend({},comment_description);
localStorage.setItem('COMMENT_DESCRIPTION',JSON.stringify(newItems));
}
console.log("added following"+localStorage.COMMENT_DESCRIPTION);
}
答案 0 :(得分:3)
您需要先解析它
JSON.parse(localStorage.COMMENT_DESCRIPTION).list_no
您的localStorage.COMMENT_DESCRIPTION
的内容是JSON字符串,还不是对象
修改你的工作小提琴