javascript - 使用 HTML5 本地存储

标签 javascript html

我正在开发一个小型网站,无法在网页上保持成本值的持久性。

我尝试过使用 Html5 存储,但无法让它工作。

这是我的 JavaScript:

function modifyPrice(cost) {
    // body...
    var total = document.getElementById('totalCost');
    var accumulte = total.value;
    var temp = cost + Number.isInteger(accumulte) + 15;
    total.value = temp;

    localStorage.setItem("priceTag",temp);

    document.getElementById('pricetag').innerHTML = localStorage.getItem("priceTag");

}

这是我的示例 html:

<span>
    <span name="price" id="pricetag"></span> $
</span>

这可能是我执行localStorage的方式还是有更好的方法?

最佳答案

Number.isInteger(accumulte) 返回 bool 结果。所以你必须将代码更改为类似

var calculate = document.getElementById('calculate');
calculate.addEventListener("click", function() {
  var total = document.getElementById('totalCost'),
    accumulte = +total.value, // casting int to string.
    cost = 10,
    temp = cost + (Number.isInteger(accumulte) ? parseInt(accumulte, 10) : 0) + 15;
  alert("Value when accumulte is " + total.value + " = " + temp);
}, false);
<input type="text" value="07" id="totalCost" />
<input type="button" value="Calculate" id="calculate" />

关于javascript - 使用 HTML5 本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41583782/

相关文章:

javascript - 在 Rails/Javascript 中渲染简单的表单部分

javascript - 有人可以建议一些更好的方法来编写这个 Javascript 代码吗

javascript - 转换 :unchecked to prop ('checked' , 错误)

javascript - 正则表达式 - +/- 符号和小数

javascript - 居中弹出消息 - Javascript

javascript - 如何在使用 Jquery 时回显 PHP session 变量?

html - 为什么DOM中Attr继承自Node?

jquery - 如何在动态创建的按钮上使用 jQuery 捕获点击事件

javascript - 如何通过网络将音乐传输到移动设备?

Javascript 时间戳