欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

目錄

購物網(wǎng)站代碼javascript 購物網(wǎng)站代碼html

// 獲取購物車列表
function getCartList() {
  // 假設(shè)購物車數(shù)據(jù)存儲在localStorage中,鍵為'cart'
  const cart = localStorage.getItem('cart');
  if (cart) {
    return JSON.parse(cart);
  } else {
    return [];
  }
}

// 添加商品到購物車
function addToCart(productId, quantity) {
  // 假設(shè)購物車數(shù)據(jù)存儲在localStorage中,鍵為'cart'
  const cart = getCartList();
  const product = document.getElementById(`product-${productId}`);
  const quantityElement = document.createElement('input');
  quantityElement.type = 'number';
  quantityElement.value = quantity;
  quantityElement.id = `quantity-${productId}`;
  quantityElement.addEventListener('change', () => {
    updateCartQuantity(productId, parseInt(quantityElement.value));
  });
  product.appendChild(quantityElement);
  cart.push({ productId, quantity: quantity });
  saveCartToLocalStorage(cart);
}

// 更新購物車數(shù)量
function updateCartQuantity(productId, quantity) {
  const cart = getCartList();
  const product = document.getElementById(`product-${productId}`);
  const quantityElement = document.getElementById(`quantity-${productId}`);
  const existingQuantity = parseInt(quantityElement.value);
  if (existingQuantity < quantity) {
    alert('庫存不足');
    return;
  }
  product.removeChild(quantityElement);
  cart[productId].quantity = quantity;
  saveCartToLocalStorage(cart);
}

// 保存購物車到localStorage
function saveCartToLocalStorage(cart) {
  localStorage.setItem('cart', JSON.stringify(cart));
}

// 初始化購物車
function initCart() {
  const cart = getCartList();
  for (const product of cart) {
    const productId = product.productId;
    const productElement = document.getElementById(`product-${productId}`);
    productElement.style.display = 'block';
    productElement.dataset.price = product.quantity * product.price;
    productElement.dataset.totalPrice = product.quantity * product.price;
  }
}

// 顯示購物車
function showCart() {
  const cart = getCartList();
  const cartContainer = document.getElementById('cart-container');
  cartContainer.innerHTML = '';
  for (const product of cart) {
    const productElement = document.createElement('div');
    productElement.classList.add('product');
    productElement.dataset.productId = product.productId;
    productElement.dataset.price = product.quantity * product.price;
    productElement.dataset.totalPrice = product.quantity * product.price;
    productElement.innerHTML = `<p>${product.productName}</p><p>${product.price}</p><button onclick="deleteProduct(${product.productId})">刪除</button>`;
    cartContainer.appendChild(productElement);
  }
}

// 刪除商品
function deleteProduct(productId) {
  const cart = getCartList();
  const index = cart.findIndex((product) => product.productId === productId);
  if (index !== -1) {
    cart.splice(index, 1);
    saveCartToLocalStorage(cart);
  } else {
    alert('商品不存在');
  }
}

// 初始化購物車并顯示
initCart();
showCart();

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/2027372857.html

發(fā)布評論

您暫未設(shè)置收款碼

請在主題配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪問

文章目錄