gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:03:09 24 4
gpt4 key购买 nike

这是我的购物车:

enter image description here

我现在遇到的问题是,总量和总计不是用新值相加而是用新值替换,以便它显示最后一个值。我想增加值(value)。

这是要在底部添加总数量和总小计的脚本:

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. But it shows 24 which is string, no math performed. How to solve this, please?

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

最佳答案

本地存储存储键和值,其中值也是字符串。如果你想对一个数值进行操作,你需要从本地存储获取它,将其解析为整数,执行操作并再次存储它:

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

关于javascript - 如何计算localStorage中的总量和总计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34512493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com