我的javascript本地存储有什么问题?

时间:2014-02-18 15:41:28

标签: javascript json local-storage

我正在编写一些使用浏览器本地存储的代码。第一个功能:

function createLocalStore(){
    if(window.localStorage.getItem("iTotal")===null){
        window.localStorage.setItem("iTotal", 0);
    }
    if(window.localStorage.getItem("mTotal")===null){
        window.localStorage.setItem("mTotal", 0);
    }
    if(window.localStorage.getItem("yTotal")===null){
        window.localStorage.setItem("yTotal", 0);
    }
    if(window.localStorage.getItem("purgeDate")===null){
        today= new Date();
        purgeDate= new Date(today.getYear(),today.getMonth(),today.getDay()+1);
        window.localStorage.setItem("purgeDate", purgeDate);
    }
    if(window.localStorage.getItem("purchIndex")===null){
        window.localStorage.setItem("purchIndex", 0);
    }
    if(window.localStorage.getItem("depIndex")===null){
        window.localStorage.setItem("depIndex", 0);
    }
}

工作得很漂亮。但是,这两个功能:

function storePurchase(n,p,d){
    index=window.localStorage.getItem("purchIndex");
    var purch={
        name1: n,
        price: p,
        date: d
    };
    if(window.localStorage.getItem("purch"+index)!==null){
        alert("storage error!");
    }
    else{
        window.localStorage.setItem("purch"+index,purch);
        window.localStorage.setItem("purchIndex", window.localStorage.getItem("purchIndex")+1);
        window.localStorage.setItem("iTotal", window.localStorage.getItem("iTotal")-window.localStorage.setItem("purch"+index));
        window.localStorage.setItem("mTotal", window.localStorage.getItem("mTotal")-window.localStorage.setItem("purch"+index));
        window.localStorage.setItem("yTotal", window.localStorage.getItem("yTotal")-window.localStorage.setItem("purch"+index));
     }
}
function storeDeposit(p,d){
     index=window.localStorage.getItem("depIndex");
     var dep={
        price: p,
        date: d
     };
     if(window.localStorage.getItem("dep"+index)!==null){
        alert("storage error!");
     }
     else{
        window.localStorage.setItem("dep"+index,dep);
        window.localStorage.setItem("depIndex", window.localStorage.getItem("depIndex")+1);
        window.localStorage.setItem("iTotal", window.localStorage.getItem("iTotal")+window.localStorage.setItem("dep"+index));
        window.localStorage.setItem("mTotal", window.localStorage.getItem("mTotal")+window.localStorage.setItem("dep"+index));
        window.localStorage.setItem("yTotal", window.localStorage.getItem("yTotal")+window.localStorage.setItem("dep"+index));
     }
}

出于某种原因不写入本地存储。我把它们称为:

var price = document.getElementById('pPrice').value;
var name = document.getElementById('pName').value;
var date=new Date();
var sDate = '<input type="hidden" class="createDate" value="'+date+'"></br>';
storePurchase(name, price, date);

var price=document.getElementById('dPrice').value;
var date=new Date();
var sDate = '<input type="hidden" class="createDate" value="'+date+'"></br>';
storeDeposit(price, date);

所以我真的不确定问题出在哪里。如果有人能够指出它,我们将不胜感激。提前感谢所有的帮助。

2 个答案:

答案 0 :(得分:2)

<强>问题

在此处调用setItem时,您没有传入值:

来自storePurchase

window.localStorage.setItem("iTotal", window.localStorage.getItem("iTotal")-window.localStorage.setItem("purch"+index));
window.localStorage.setItem("mTotal", window.localStorage.getItem("mTotal")-window.localStorage.setItem("purch"+index));
window.localStorage.setItem("yTotal", window.localStorage.getItem("yTotal")-window.localStorage.setItem("purch"+index));

//The invalid portion from above:
window.localStorage.setItem("purch"+index)

来自storeDeposit

window.localStorage.setItem("iTotal", window.localStorage.getItem("iTotal")+window.localStorage.setItem("dep"+index));
window.localStorage.setItem("mTotal", window.localStorage.getItem("mTotal")+window.localStorage.setItem("dep"+index));
window.localStorage.setItem("yTotal", window.localStorage.getItem("yTotal")+window.localStorage.setItem("dep"+index));

//The invalid portion from above:
window.localStorage.setItem("dep"+index)

<强>解决方案

您可能认为这些是对getItem的调用。

来自storePurchase

window.localStorage.setItem("iTotal", window.localStorage.getItem("iTotal")-window.localStorage.getItem("purch"+index));
window.localStorage.setItem("mTotal", window.localStorage.getItem("mTotal")-window.localStorage.getItem("purch"+index));
window.localStorage.setItem("yTotal", window.localStorage.getItem("yTotal")-window.localStorage.getItem("purch"+index));

来自storeDeposit

window.localStorage.setItem("iTotal", window.localStorage.getItem("iTotal")+window.localStorage.getItem("dep"+index));
window.localStorage.setItem("mTotal", window.localStorage.getItem("mTotal")+window.localStorage.getItem("dep"+index));
window.localStorage.setItem("yTotal", window.localStorage.getItem("yTotal")+window.localStorage.getItem("dep"+index));

答案 1 :(得分:0)

我认为你不希望在传递对象保存

时localStorage如何工作
var purch={
    name1: n,
    price: p,
    date: d
};

window.localStorage.setItem("purch1",purch);

现在当你打电话

window.localStorage.getItem("purch1");
// result [object Object]

必须像这样

// to set json
window.localStorage.setItem("purch1",JSON.stringify(purch));
// to get json
var data = JSON.parse(window.localStorage.getItem("purch1"));

为什么你不编写自己的代码来获取/设置数据(因为我看到大量代码,不可重复!)

function set(key, value){
  if(key && value){
    /* serialize */
    if(typeof value == "object")
      value = JSON.stringify(value);
    try{
      localStorage.setItem(key, value);
    }
    catch(e){
      if(e == QUOTA_EXCEEDED_ERR){
        console.log("MAX_SIZE_ERROR", e);
      }
      console.log("ERROR", e);
    }
  }
  else console.warn("Must be 2 params", key, value);
  return null;
}

function get(key){
  var item = localStorage.getItem(key);
  if(!item) return item;

  try{
    /* check that string is object*/
    item = JSON.parse(item);
  }
  catch(e){}
  return item;
}

现在您可以使用此代码编写(只是示例,编写您自己的内容以了解localStorage如何工作,这将是很棒的做法!)

set("purch"+index, purch);