小编God*_*osn的帖子

如何更新总价并在更新后保留新数量?

我正在创建一个购物车,我希望在单击更新按钮时更新总价。另外,我希望新的数量和总价格在刷新页面后保持不变。

我正在使用wamp服务器。

显示代码:

var products = JSON.parse(localStorage.getItem('products'));
var shoppingCart = JSON.parse(localStorage.getItem('shoppingCart'));
var itemQuantity;

function showItem(){
    var total=0;
    for(var i in shoppingCart) {
        var id = shoppingCart[i]; 
        var itemImage = products[id].image;
        var itemPrice = products[id].price;
        var itemDescription = products[id].description;
        itemQuantity=1;
        var totalPrice = parseInt(itemPrice*itemQuantity);

        var index = "<tr><td><img 
               src='img/"+itemImage+"'width='100px'/></td> 
               <td>"+itemDescription + "</td><td>" + itemPrice</td>
               <td><input id="+id+" type=number min=1 max=100 
               value="+itemQuantity+" /></td>
               <td>"+totalPrice+"</td>
               <td>"+ "<button 
               onclick='removeItem("+i+")'>Remove</button></td></tr>";

        $('#product_list').append(index);
        total += (totalPrice);
    } 
    document.getElementById('total').innerHTML = total;
    document.querySelector('.badge').innerHTML = shoppingCart.length;
}
showItem();
Run Code Online (Sandbox Code Playgroud)

更新总价: …

javascript wamp

5
推荐指数
0
解决办法
69
查看次数

标签 统计

javascript ×1

wamp ×1