【问题标题】:jquery shopping cart delete table row from local storagejquery 购物车从本地存储中删除表行
【发布时间】:2017-10-21 22:16:14
【问题描述】:

当我点击“x”按钮时,我正在尝试从购物车(和本地存储)中删除一行。

这是我目前所拥有的:

for (i=0; i<numCart; i++){

    var button = $('<td>'+infoCartItems[i].button+'</td>').addClass("checkoutTitles");
    button.text('x');
    row.append(button);
    $(".cartTable").append(row);

}

这是我的存储代码:

var newItem= new Bun(pack, current, flavor2nd, flavor3rd, money);
var existingCartItems = JSON.parse(localStorage.getItem("itemsArray"))||[];
existingCartItems.push(newItem);
localStorage.setItem("itemsArray", JSON.stringify(existingCartItems));

【问题讨论】:

  • 当前行为和预期行为是什么?
  • 目前,当我点击“x”时没有任何反应

标签: javascript jquery local-storage shopping-cart


【解决方案1】:

这是我用来从购物车中删除商品的代码:

$(".remove").on('click', function() {

// get the index of the row in the table
var index = $(this).parent().parent().index();
index = index - 1;

// remove item from table
$(this).parent().parent().remove();

// remove item from the cart local storage
var cart = JSON.parse(localStorage.getItem("itemsArray"));
cart.splice(index, 1);
localStorage.setItem("itemsArray", JSON.stringify(cart));
location.reload();

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2017-01-29
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    相关资源
    最近更新 更多