简体   繁体   English

如何计算localStorage中的总量和总量?

[英]How to calculate total quantity and total in localStorage?

This is my cart: 这是我的推车:

在此处输入图片说明

The issue I'm having now is, total quantity and total not adding with new value but replacing with new value, so that it shows the last value. 我现在遇到的问题是,总数和总数未添加新值,而是替换为新值,以便显示最后一个值。 I want to add up the value. 我想加总价值。

This is the script to add for total quantity and total subtotal in the bottom: 这是要在底部添加总数量和总小计的脚本:

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);


       }


//I'm trying to add the values in localStorage but it shows in string form.For instance when first item's quantity key in '2' it should show 2. WHen next item quantity keyed in '4' it must add prev value and new value. //我正在尝试在localStorage中添加值,但是它以字符串形式显示。例如,当第一个项目的数量键为“ 2”时,它应该显示2。如果下一个项目数量为“ 4”,则必须添加上一个值,并且新价值。 But it shows 24 which is string, no math performed. 但它显示24,这是字符串,未执行数学运算。 How to solve this, please? 请问如何解决?

           localStorage.quantity += parseInt(qty);
           alert(localStorage.quantity);

Local storage store keys and values, where values are also strings. 本地存储存储键和值,其中值也是字符串。 If you want to operate with a numeric value you need to get it from the Local storage , parse it to integer, perform the operation and store it again: 如果要使用数字值进行操作,则需要从本地存储中获取它,将其解析为整数,执行该操作并再次存储它:

var value = parseInt(localStorage.getItem("value"));
value += 5;
localStorage.setItem("value", value);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM