[英]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.