简体   繁体   English

如何在不重新加载页面的情况下更新 localStorage?

[英]How to update localStorage without reload the page?

I am building a shopping cart with products fetched from a json-server.我正在构建一个购物车,其中包含从 json-server 获取的产品。 I have managed to display the products, implement the add to cart function and display the quantity, however, I am now stuck at the increment and decrement fucntion.我已经设法展示了产品,实现了添加到购物车 function 并显示了数量,但是,我现在卡在了增量和减量功能上。 The idea is everytime the user click on the + and - buttons, the product.count property change (which I use it to display it on the cart table).这个想法是每次用户单击 + 和 - 按钮时,product.count 属性都会更改(我用它来将其显示在购物车表上)。 Here is my code这是我的代码

load cart from localstorage
function loadCart() {
    let products = JSON.parse(localStorage.getItem('products'));
    let cartTotalItem = document.querySelector('.cart__value');
    cartTotalItem.textContent = products.length;
    products.forEach(product => {
        let cartItem = document.createElement('tr');
        cartItem.innerHTML = `<td><img src="${product.imgSrc}" alt='product image'></td>
                  <td>${product.name}</td>
                  <td><span class='cart-price'>${product.price},000vnd</span></td>
                  <td>
                    <button action="remove" onclick='decrease()'>-</button>
                    <input name='product' class="product__quantity" type="number" min='1' max='100' value='${product.count}'>
                    <button action="add" onclick='change()' >+</button>
                  </td>
                  <td><span class='cart-total-value'>${parseInt(product.price) * product.count},000vnd</span></td>
                  <td>
                    <button class="delete--item"><i class="far fa-trash-alt"></i></button>
                  </td>`;
        tableBody.appendChild(cartItem);
    });
}

My attempt to implement the function我尝试实现 function


function change() {
    let products = JSON.parse(localStorage.getItem('products'));
    for (let i = 0; i < products.length; i++) {
        let inputValue = document.querySelector('.product__quantity').textContent
        inputValue = products[i].count++
        localStorage.setItem('products', JSON.stringify(products))
    }
}

function decrease() {
    let products = JSON.parse(localStorage.getItem('products'));
    for (let i = 0; i < products.length; i++) {
        let inputValue = document.querySelector('.product__quantity').value
        inputValue = products[i].count--
        localStorage.setItem('products', JSON.stringify(products))
    }
}

The problem is I have to reload the page again in order for the change to reflect on the page.问题是我必须再次重新加载页面才能使更改反映在页面上。 Can anyone help me please?任何人都可以帮助我吗?

I think you need to call loadCart() again after products in localStorage is changed.我认为您需要在 localStorage 中的产品更改后再次调用 loadCart() 。

function change() {
    let products = JSON.parse(localStorage.getItem('products'));
    for (let i = 0; i < products.length; i++) {
        let inputValue = document.querySelector('.product__quantity').textContent
        inputValue = products[i].count++
        localStorage.setItem('products', JSON.stringify(products))
    }
    loadCart()
}

function decrease() {
    let products = JSON.parse(localStorage.getItem('products'));
    for (let i = 0; i < products.length; i++) {
        let inputValue = document.querySelector('.product__quantity').value
        inputValue = products[i].count--
        localStorage.setItem('products', JSON.stringify(products))
    }
    loadCart()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM