如何用JavaScript编写购物车金额计算功能?

更新于
2026-09-26 16:23:07
1阅读来源:SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1014个文字,预计阅读时间需要5分钟。

如何用JavaScript编写购物车金额计算功能?

原文示例代码如下:

javascript// 购物车计算器实现// 以店铺为单元,每个店铺下面有对应的商品选项,可全选,商品单选,店铺单选,商品数量加减,删除// 初始化店铺和商品数据let stores=[ { name: 店铺1, products: [ { name: 商品1, price: 100 }, { name: 商品2, price: 200 } ] }, { name: 店铺2, products: [ { name: 商品3, price: 150 }, { name: 商品4, price: 250 } ] }];

// 购物车数据let cart={ selectedStore: null, selectedProduct: null, quantity: 0};

// 显示店铺和商品function displayStoresAndProducts() { let=; stores.forEach(store=> { +=`${store.name}`; }); +=;

+=; if (cart.selectedStore) { stores.forEach(store=> { if (store.name===cart.selectedStore) { store.products.forEach(product=> { +=`${product.name}`; }); } }); } +=;

document.getElementById(storeAndProduct).innerHTML=;}

// 更新商品列表function updateProductList() { let selectedStore=document.getElementById(storeSelect).value; cart.selectedStore=selectedStore; displayStoresAndProducts();}

// 更新商品数量function updateQuantity() { let quantityInput=document.getElementById(quantityInput); cart.quantity=parseInt(quantityInput.value);}

// 添加到购物车function addToCart() { let selectedProduct=document.getElementById(productSelect).value; cart.selectedProduct=selectedProduct; updateQuantity(); // 这里可以添加到购物车的逻辑}

// 显示购物车function displayCart() { let total=0; stores.forEach(store=> { store.products.forEach(product=> { if (product.name===cart.selectedProduct) { total +=product.price * cart.quantity; } }); }); document.getElementById(cartTotal).innerText=总价: + total;}

// 页面加载时显示店铺和商品window.onload=function() { displayStoresAndProducts();};

简化后的代码:

javascript// 购物车计算器简化版// 以店铺为单元,每个店铺下面有对应的商品选项,可全选,商品单选,店铺单选,商品数量加减,删除let stores=[ { name: 店铺1, products: [{ name: 商品1, price: 100 }, { name: 商品2, price: 200 }] }, { name: 店铺2, products: [{ name: 商品3, price: 150 }, { name: 商品4, price: 250 }] }];

let cart={ selectedStore: null, selectedProduct: null, quantity: 0 };

function display() { let=; stores.forEach(store=> +=`${store.name}`); +=; +=; if (cart.selectedStore) { stores.forEach(store=> { if (store.name===cart.selectedStore) { store.products.forEach(product=> +=`${product.name}`); } }); } +=; document.getElementById(storeAndProduct).innerHTML=;}

如何用JavaScript编写购物车金额计算功能?

function update() { let selectedStore=document.getElementById(storeSelect).value; cart.selectedStore=selectedStore; display();}

function add() { let selectedProduct=document.getElementById(productSelect).value; cart.selectedProduct=selectedProduct; let quantityInput=document.getElementById(quantityInput); cart.quantity=parseInt(quantityInput.value);}

function show() { let total=0; stores.forEach(store=> { store.products.forEach(product=> { if (product.name===cart.selectedProduct) { total +=product.price * cart.quantity; } }); }); document.getElementById(cartTotal).innerText=总价: + total;}

window.onload=display;

本文实例为大家分享了js实现购物车计算的具体代码,供大家参考,具体内容如下

以店铺为单位,每个店铺下面有对应的商品选项,可全选,商品单选,店铺单选,商品数量加减,删除

代码:

<script> export default { data() { return { count:0, //所有商品的数量和 shopCarList: [], //所有商品 selectList:[] //已选择的商品 } }, computed: { //选择的合计价格 selectPrice(){ return this.selectList.reduce((total,item,index)=>{ return total+item.quantity*(item.activityPrice || item.price); },0) }, //选择的合计数量 selectNum(){ return this.selectList.reduce((total,item,index)=>{ return total+item.quantity; },0) }, //是否全选 isSelecetAll(){ return this.shopCarList.every(item=>{ return item.shopSelect===true }) } }, methods: { //增加 increment(itm){ itm.quantity<itm.stock?itm.quantity++:''; }, //减少 decrement(itm){ itm.quantity>1?itm.quantity--:'' }, //全选点击 allChooseHandle(){ this.initSelectAll(!this.isSelecetAll); }, //商品单选点击 productChooseHandle(itm,index){ let productFlag = !itm.productSelect; this.$set(itm,'productSelect',productFlag); let shopFlag = this.shopCarList[index].products.every(i=>{ return i.productSelect===true; }) this.$set(this.shopCarList[index],'shopSelect',shopFlag); this.getSelected(); }, //店铺单选点击 shopChooseHandle(item,index){ let Flag = !item.shopSelect; this.$set(item,'shopSelect',Flag); item.products.forEach((itm,idx)=>{ this.$set(itm,'productSelect',Flag); }) this.getSelected(); }, //获取已选商品 getSelected(){ let tempArr = []; this.shopCarList.forEach((item,index)=>{ item.products.forEach((itm,idx)=>{ if(itm.productSelect){ tempArr.push(itm); } }) }) this.selectList = tempArr; }, //是否全选初始化 initSelectAll(Flag){ this.shopCarList.forEach((item,index)=>{ this.$set(item,'shopSelect',Flag); item.products.forEach((itm,idx)=>{ this.$set(itm,'productSelect',Flag); }) }) this.getSelected(); }, //获取购物车列表 getCarList(){ return this.$api.personal.getCarList().then(res=>{ if(res.result==='000'){ //过期商品数 let expiredNum = 0; res.data.expiredList.forEach(item=>{ item.products.forEach(itm=>{ expiredNum += itm.quantity }) }) //有效商品数量 this.count = res.data.count - expiredNum; this.shopCarList = res.data.shopCarList; return Promise.resolve() } }) }, //删除点击 delCarList(itm,index,idx){ this.$api.personal.delCarList( [itm.carId] ).then(res=>{ if(res.result==='000'){ this.count -= itm.quantity; this.shopCarList[index].products.splice(idx,1); if(this.shopCarList[index].products.length===0){ this.shopCarList.splice(index,1); } this.getSelected(); } }) }, }, created() { this.getCarList().then(()=>{ this.initSelectAll(true); }); }, }; </script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

本文共计1014个文字,预计阅读时间需要5分钟。

如何用JavaScript编写购物车金额计算功能?

原文示例代码如下:

javascript// 购物车计算器实现// 以店铺为单元,每个店铺下面有对应的商品选项,可全选,商品单选,店铺单选,商品数量加减,删除// 初始化店铺和商品数据let stores=[ { name: 店铺1, products: [ { name: 商品1, price: 100 }, { name: 商品2, price: 200 } ] }, { name: 店铺2, products: [ { name: 商品3, price: 150 }, { name: 商品4, price: 250 } ] }];

// 购物车数据let cart={ selectedStore: null, selectedProduct: null, quantity: 0};

// 显示店铺和商品function displayStoresAndProducts() { let=; stores.forEach(store=> { +=`${store.name}`; }); +=;

+=; if (cart.selectedStore) { stores.forEach(store=> { if (store.name===cart.selectedStore) { store.products.forEach(product=> { +=`${product.name}`; }); } }); } +=;

document.getElementById(storeAndProduct).innerHTML=;}

// 更新商品列表function updateProductList() { let selectedStore=document.getElementById(storeSelect).value; cart.selectedStore=selectedStore; displayStoresAndProducts();}

// 更新商品数量function updateQuantity() { let quantityInput=document.getElementById(quantityInput); cart.quantity=parseInt(quantityInput.value);}

// 添加到购物车function addToCart() { let selectedProduct=document.getElementById(productSelect).value; cart.selectedProduct=selectedProduct; updateQuantity(); // 这里可以添加到购物车的逻辑}

// 显示购物车function displayCart() { let total=0; stores.forEach(store=> { store.products.forEach(product=> { if (product.name===cart.selectedProduct) { total +=product.price * cart.quantity; } }); }); document.getElementById(cartTotal).innerText=总价: + total;}

// 页面加载时显示店铺和商品window.onload=function() { displayStoresAndProducts();};

简化后的代码:

javascript// 购物车计算器简化版// 以店铺为单元,每个店铺下面有对应的商品选项,可全选,商品单选,店铺单选,商品数量加减,删除let stores=[ { name: 店铺1, products: [{ name: 商品1, price: 100 }, { name: 商品2, price: 200 }] }, { name: 店铺2, products: [{ name: 商品3, price: 150 }, { name: 商品4, price: 250 }] }];

let cart={ selectedStore: null, selectedProduct: null, quantity: 0 };

function display() { let=; stores.forEach(store=> +=`${store.name}`); +=; +=; if (cart.selectedStore) { stores.forEach(store=> { if (store.name===cart.selectedStore) { store.products.forEach(product=> +=`${product.name}`); } }); } +=; document.getElementById(storeAndProduct).innerHTML=;}

如何用JavaScript编写购物车金额计算功能?

function update() { let selectedStore=document.getElementById(storeSelect).value; cart.selectedStore=selectedStore; display();}

function add() { let selectedProduct=document.getElementById(productSelect).value; cart.selectedProduct=selectedProduct; let quantityInput=document.getElementById(quantityInput); cart.quantity=parseInt(quantityInput.value);}

function show() { let total=0; stores.forEach(store=> { store.products.forEach(product=> { if (product.name===cart.selectedProduct) { total +=product.price * cart.quantity; } }); }); document.getElementById(cartTotal).innerText=总价: + total;}

window.onload=display;

本文实例为大家分享了js实现购物车计算的具体代码,供大家参考,具体内容如下

以店铺为单位,每个店铺下面有对应的商品选项,可全选,商品单选,店铺单选,商品数量加减,删除

代码:

<script> export default { data() { return { count:0, //所有商品的数量和 shopCarList: [], //所有商品 selectList:[] //已选择的商品 } }, computed: { //选择的合计价格 selectPrice(){ return this.selectList.reduce((total,item,index)=>{ return total+item.quantity*(item.activityPrice || item.price); },0) }, //选择的合计数量 selectNum(){ return this.selectList.reduce((total,item,index)=>{ return total+item.quantity; },0) }, //是否全选 isSelecetAll(){ return this.shopCarList.every(item=>{ return item.shopSelect===true }) } }, methods: { //增加 increment(itm){ itm.quantity<itm.stock?itm.quantity++:''; }, //减少 decrement(itm){ itm.quantity>1?itm.quantity--:'' }, //全选点击 allChooseHandle(){ this.initSelectAll(!this.isSelecetAll); }, //商品单选点击 productChooseHandle(itm,index){ let productFlag = !itm.productSelect; this.$set(itm,'productSelect',productFlag); let shopFlag = this.shopCarList[index].products.every(i=>{ return i.productSelect===true; }) this.$set(this.shopCarList[index],'shopSelect',shopFlag); this.getSelected(); }, //店铺单选点击 shopChooseHandle(item,index){ let Flag = !item.shopSelect; this.$set(item,'shopSelect',Flag); item.products.forEach((itm,idx)=>{ this.$set(itm,'productSelect',Flag); }) this.getSelected(); }, //获取已选商品 getSelected(){ let tempArr = []; this.shopCarList.forEach((item,index)=>{ item.products.forEach((itm,idx)=>{ if(itm.productSelect){ tempArr.push(itm); } }) }) this.selectList = tempArr; }, //是否全选初始化 initSelectAll(Flag){ this.shopCarList.forEach((item,index)=>{ this.$set(item,'shopSelect',Flag); item.products.forEach((itm,idx)=>{ this.$set(itm,'productSelect',Flag); }) }) this.getSelected(); }, //获取购物车列表 getCarList(){ return this.$api.personal.getCarList().then(res=>{ if(res.result==='000'){ //过期商品数 let expiredNum = 0; res.data.expiredList.forEach(item=>{ item.products.forEach(itm=>{ expiredNum += itm.quantity }) }) //有效商品数量 this.count = res.data.count - expiredNum; this.shopCarList = res.data.shopCarList; return Promise.resolve() } }) }, //删除点击 delCarList(itm,index,idx){ this.$api.personal.delCarList( [itm.carId] ).then(res=>{ if(res.result==='000'){ this.count -= itm.quantity; this.shopCarList[index].products.splice(idx,1); if(this.shopCarList[index].products.length===0){ this.shopCarList.splice(index,1); } this.getSelected(); } }) }, }, created() { this.getCarList().then(()=>{ this.initSelectAll(true); }); }, }; </script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。