私はこれまでのところカートで作業していますが、ミニカートでdeleteをクリックすると、localStorageと空の配列をチェックできないようですいくつかの要素をリセットします。ページを更新すると、カートの数が0に、価格が0.00に変更されますが、削除をクリックすると更新されません。私はリフレッシュして苦労しなければならない。jQuery localStorageと空の場合null/nullをリフレッシュする要素をリフレッシュする
削除アクション:
$(document).on('click', '.delete-item', function(event) {
event.preventDefault(); // disable normal link function so that it doesn't refresh the page
var item = $(this).data('item'); // get data item to delete
deleteItem(item);
$(".basket").hide();
});
機能:
function deleteItem(index){
if (localStorage.basket) {
basket = JSON.parse(localStorage.basket);
basket.splice(index,1); // delete item at index
saveBasket();
updateBasket();
}
return;
}
セーブ機能:
// Save the basket
function saveBasket(){
if (window.localStorage){
localStorage.basket = JSON.stringify(basket);
}
}
更新機能:
function updateBasket(){
$.each(basket, function(product) {
//console.log(basket);
// update the basket count
//basket = JSON.parse(localStorage.basket);
//if(basket[product].qty === "0" || basket === null || basket.length === 0){
if(basket[product].qty === "0"){
$('.mini-cart .count').html("0");
} else {
$('.mini-cart .count').html(basket[product].qty); // set the cart amount
}
// update the basket total
if(basket[product].price > 0){
var finalPrice = basket[product].price * basket[product].qty; // calculate the price and how many
$('.mini-cart .price').html("£"+finalPrice);
} else {
$('.mini-cart .price').html("Basket is empty");
}
});
return;
}
私が言ったように、予想通りアイテムを削除していますが、カウントと価格の要素を更新したいと考えています。どのようなヘルプのために事前に感謝します。 – James