页面加载后JavaScript localStorage无法保存数据

页面加载后JavaScript localStorage无法保存数据,javascript,jquery,local-storage,shopping-cart,Javascript,Jquery,Local Storage,Shopping Cart,我有一个迷你的签出JavaScript购物车,在用户获得他们的总金额后,他们输入他们支付的金额,然后返回他们的更改。为了做到这一点,我需要在页面加载后存储“total”变量值。这就是我使用localStorage的原因。但是,它不起作用。有人能帮我吗 var itemsSelectedValues = []; // variable above is empty array that will ultimately store the items user selected to p

我有一个迷你的签出JavaScript购物车,在用户获得他们的总金额后,他们输入他们支付的金额,然后返回他们的更改。为了做到这一点,我需要在页面加载后存储“total”变量值。这就是我使用localStorage的原因。但是,它不起作用。有人能帮我吗

var itemsSelectedValues = [];
      // variable above is empty array that will ultimately store the items user selected to purchase
      // var total = 0;
      localStorage.setItem("total", 0);
      // variable above is set to 0 that will ultimately represent total of user purchase

      function listOfCheckedItems(catalogToLookFrom) {
        // line above defines function listOfCheckedItems, with parameter catalogToLookFrom being passed in to represent array object "shoppingItems"
        var yourItemsPurchased = $("input:checkbox:checked");
        // line above sets variable yourItemsPurchased to all the checkbox items that were checked
        for (i = 0; i < yourItemsPurchased.length; i++) {
          // line above is a for loop that loops through the entire length of array "yourItemsPurchased"
             itemsSelectedValues.push(yourItemsPurchased[i].value);
             // line above pushes the value of items in array "yourItemsPurchased" into empty array "itemsSelectedValues"
             // this action occurs each time through the loop
        } // line closes for loop
        console.log(itemsSelectedValues);
        for (i = 0; i < itemsSelectedValues.length; i++) {
          // line above loops through array itemsSelectedValues (entire length of array)
          localStorage.getItem("total") = localStorage.getItem("total"); + catalogToLookFrom[itemsSelectedValues[i]];
          // line above sets the variable "total" to itself plus the value of the item name that's in catalogToLookFrom (which represents array object "shoppingItems")
          // this happens each time through the loop
          // so 1st time "total" is set to itself (which is initially 0) plus the value of whatever the 1st item is. Then so on.....
        } // closes for loop
        document.getElementById("yourPurchase").innerHTML = "Your items purchased <strong>" + itemsSelectedValues + "</strong> came to a total of <br/><strong>$" + localStorage.getItem("total"); + "</strong>";
      } // line closes listOfCheckedItems function

      function getOrderTotal() {
        listOfCheckedItems(shoppingItems);
      }

///////// Part not working

  function whatUserPaidWith() {
    var amountUserPaid = $("#amountPaid").val();
    // line above creates variable "amountUserPaid" which is set to the value of element with an id of "#amountPaid"
    var customerChange = amountUserPaid - localStorage.getItem("total");
    document.getElementById("displayUserChange").innerHTML = "You paid <strong>" + amountUserPaid + "</strong> your change due is <strong>" + customerChange + "</strong>";

  }
var itemsSelectedValues=[];
//上面的变量是空数组,它将最终存储用户选择购买的项目
//var合计=0;
localStorage.setItem(“总计”,0);
//上述变量设置为0,最终表示用户购买的总金额
checkedItems的函数列表(catalogToLookFrom){
//上面的行定义了checkedItems的函数listOfCheckedItems,传入参数catalogToLookFrom表示数组对象“shoppingItems”
var yourItemsPurchased=$(“输入:复选框:选中”);
//上面的行将变量yourItemsPurchased设置为选中的所有复选框项
对于(i=0;i“+itemsSelectedValues+”总计为
$”+localStorage.getItem(“总计”);+“”; }//行关闭listOfCheckedItems函数 函数getOrderTotal(){ 检查清单(购物物品); } /////////零件不工作 函数whatUserPaidWith(){ var amountUserPaid=$(“#amountPaid”).val(); //上面的行创建变量“amountUserPaid”,该变量设置为id为“#amountPaid”的元素的值 var customerChange=amountUserPaid-localStorage.getItem(“总计”); document.getElementById(“displayUserChange”).innerHTML=“您已付款”“+amountUserPaid+”您的到期更改是“+customerChange+””; }
我发现了几个问题;首先,您没有使用新的总数调用
setItem
。您应该执行以下操作来设置总数:

function listOfCheckedItems(catalogToLookFrom) {
  var yourItemsPurchased = $("input:checkbox:checked"),
      itemsSelectedValues = [],
      total = 0
  for (i = 0; i < yourItemsPurchased.length; i++) {
      itemsSelectedValues[i] = yourItemsPurchased[i].value
      total += catalogToLookFrom[yourItemsPurchased[i].value];
  }
  localStorage.setItem("total", total)
  document.getElementById("yourPurchase").innerHTML = "Your items purchased <strong>" + itemsSelectedValues.join(',') + "</strong> came to a total of <br/><strong>$" + total + "</strong>";
} 

我发现以下代码行有问题:

localStorage.getItem("total") = localStorage.getItem("total"); + catalogToLookFrom[itemsSelectedValues[i]];
我希望我的以下建议能对你有所帮助

首先,在语句的中间不应该有一个半冒号。 其次,

localStorage.getItem(“total”)
不能用作存储值的变量(正如您正在尝试的那样)。相反,
localStorage.getItem()
是一个返回值的方法

这里有一段引用自

存储接口的getItem()方法,当传递键名时, 将返回该键的值,如果该键不存在,则返回null

因此,我的建议是将上述代码行更改为以下内容:

var newTotal = localStorage.getItem("total") + catalogToLookFrom[itemsSelectedValues[i]];
localStorage.setItem("total", newTotal);
// lines above set the local storage variable "total" to itself plus the value of the item name that's in catalogToLookFrom (which represents array object "shoppingItems")

我希望这有帮助:)

控制台中是否有任何错误?您还可以包括您在控制台中得到的确切错误。我认为您需要将总数转换为如下数字:number(amountUserPaid)-number(localStorage.getItem(“total”)),其中有以下行:localStorage.getItem(“total”)=localStorage.getItem(“total”);+目录从[itemsSelectedValues[i]]开始;我认为你不能像那样储存价值观。请将其更改为以下内容并重试:var newTotal=localStorage.getItem(“total”)+catalogtookfrom[itemsSelectedValues[i];localStorage.setItem(“总计”,新总计);让我知道我的回答是否有帮助。非常感谢。
var newTotal = localStorage.getItem("total") + catalogToLookFrom[itemsSelectedValues[i]];
localStorage.setItem("total", newTotal);
// lines above set the local storage variable "total" to itself plus the value of the item name that's in catalogToLookFrom (which represents array object "shoppingItems")