[英]How to calculate total quantity and total in localStorage?
这是我的推车:
我现在遇到的问题是,总数和总数未添加新值,而是替换为新值,以便显示最后一个值。 我想加总价值。
这是要在底部添加总数量和总小计的脚本:
function calTotal(param)
{
//this carries the latest value for quantity
var qty = $('td.qty_'+param+' input[type=text]').val();
//this carries the latest value for subtotal
var total = $('span.sub_total_'+param+'').text();
//this shows the values respectively in the white row below
$('span.qty_1').text(qty);
$('span.total').text(total);
//I'm trying to add the values in localStorage but it shows in string form.
localStorage.quantity += parseInt(qty);
alert(localStorage.quantity);
}
//我正在尝试在localStorage中添加值,但是它以字符串形式显示。例如,当第一个项目的数量键为“ 2”时,它应该显示2。如果下一个项目数量为“ 4”,则必须添加上一个值,并且新价值。 但它显示24,这是字符串,未执行数学运算。 请问如何解决?
localStorage.quantity += parseInt(qty);
alert(localStorage.quantity);
本地存储存储键和值,其中值也是字符串。 如果要使用数字值进行操作,则需要从本地存储中获取它,将其解析为整数,执行该操作并再次存储它:
var value = parseInt(localStorage.getItem("value"));
value += 5;
localStorage.setItem("value", value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.