如何为cordova app启用本地存储

时间:2017-01-10 06:43:25

标签: javascript cordova

我正在尝试将值存储到混合应用程序中的本地存储中。当我在桌面Web浏览器中运行应用程序时,但是当我在Android设备中运行相同时,我无法看到与Web浏览器中相同的输出。我认为它没有被保存在本地存储中。

有什么办法可以解决这个问题,还是我必须启用本地存储?

这是我的代码:

function deductBalance(){
    var str;
    var num_arr = [];
    var elems = document.querySelectorAll('.cost-block-deduct');
    elems.forEach(function(el) {
      var nums = el.options[el.selectedIndex].value;
      num_arr.push(nums)
    });
    console.log(num_arr);
    var alvar1 = (Number.parseInt(num_arr.join("")));
    localStorage.setItem("deducted", alvar1);
    window.location.href = "home.html"
}

function todaysExpenses(){
    document.getElementById("spent_points_today").innerHTML = localStorage.deducted ;
    document.getElementById("earned_points_today").innerHTML = localStorage.balance;
    document.getElementById("balance-remaining").innerHTML = localStorage.balance - localStorage.deducted;
    document.getElementById("balance-remaining-month").innerHTML = localStorage.balance - localStorage.deducted;
}

1 个答案:

答案 0 :(得分:1)

您必须使用 localStorage.getItem() 功能

或, localStorage ["已扣除"]

function todaysExpenses(){
    var deducted = parseFloat(localStorage.getItem("deducted"));
    var balance = parseFloat(localStorage.getItem("balance"));
    document.getElementById("spent_points_today").innerHTML = deducted;
    document.getElementById("earned_points_today").innerHTML = balance;
    document.getElementById("balance-remaining").innerHTML = (balance - deducted);
    document.getElementById("balance-remaining-month").innerHTML = (balance - deducted);
}