localStorage实现购物车
来源:互联网 发布:python中文下载 编辑:程序博客网 时间:2024/06/07 10:24
shopCart.js
utils = { setParam : function (name,value){ localStorage.setItem(name,value) }, getParam : function(name){ return localStorage.getItem(name) }}product={ id:0, name:"", num:0, price:0.0,};orderdetail={ username:"", phone:"", address:"", zipcode:"", totalNumber:0, totalAmount:0.0}cart = { //向购物车中添加商品 addProduct:function(product){ var ShoppingCart = utils.getParam("ShoppingCart"); if(ShoppingCart==null||ShoppingCart==""){ //第一次加入商品 var jsonstr = {"productlist":[{"id":product.id,"name":product.name,"num":product.num,"price":product.price}],"totalNumber":product.num,"totalAmount":(product.price*product.num)}; utils.setParam("ShoppingCart","'"+JSON.stringify(jsonstr)); }else{ var jsonstr = JSON.parse(ShoppingCart.substr(1,ShoppingCart.length)); var productlist = jsonstr.productlist; var result=false; //查找购物车中是否有该商品 for(var i in productlist){ if(productlist[i].id==product.id){ productlist[i].num=parseInt(productlist[i].num)+parseInt(product.num); result = true; } } if(!result){ //没有该商品就直接加进去 productlist.push({"id":product.id,"name":product.name,"num":product.num,"price":product.price}); } //重新计算总价 jsonstr.totalNumber=parseInt(jsonstr.totalNumber)+parseInt(product.num); jsonstr.totalAmount=parseFloat(jsonstr.totalAmount)+(parseInt(product.num)*parseFloat(product.price)); orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; //保存购物车 utils.setParam("ShoppingCart","'"+JSON.stringify(jsonstr)); } }, //修改给买商品数量 updateProductNum:function(id,num){ var ShoppingCart = utils.getParam("ShoppingCart"); var jsonstr = JSON.parse(ShoppingCart.substr(1,ShoppingCart.length)); var productlist = jsonstr.productlist; for(var i in productlist){ if(productlist[i].id==id){ jsonstr.totalNumber=parseInt(jsonstr.totalNumber)+(parseInt(num)-parseInt(productlist[i].num)); jsonstr.totalAmount=parseFloat(jsonstr.totalAmount)+((parseInt(num)*parseFloat(productlist[i].price))-parseInt(productlist[i].num)*parseFloat(productlist[i].price)); productlist[i].num=parseInt(num); orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; utils.setParam("ShoppingCart","'"+JSON.stringify(jsonstr)); return; } } }, //获取购物车中的所有商品 getProductList:function(){ var ShoppingCart = utils.getParam("ShoppingCart"); var jsonstr = JSON.parse(ShoppingCart.substr(1,ShoppingCart.length)); var productlist = jsonstr.productlist; orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; return productlist; }, //不传id则判断购物车中是否存在商品,传id则判断购物车中是否存在某种商品 existProduct:function(id){ var ShoppingCart = utils.getParam("ShoppingCart"); var jsonstr = JSON.parse(ShoppingCart.substr(1,ShoppingCart.length)); var productlist = jsonstr.productlist; var result=false; if(id!=null){ for(var i in productlist){ if(productlist[i].id==id){ result = true; } } }else{ if(productlist.length>0){ result=true; } } return result; }, //不传id则删除购物车中所有商品,传id则删除某种商品 clearProduct:function(id){ var ShoppingCart = utils.getParam("ShoppingCart"); var jsonstr = JSON.parse(ShoppingCart.substr(1,ShoppingCart.length)); var productlist = jsonstr.productlist; var list=[]; if(id!=null){ for(var i in productlist){ if(productlist[i].id==id){ jsonstr.totalNumber=parseInt(jsonstr.totalNumber)-parseInt(productlist[i].num); jsonstr.totalAmount=parseFloat(jsonstr.totalAmount)-parseInt(productlist[i].num)*parseFloat(productlist[i].price); }else{ list.push(productlist[i]); } } } jsonstr.productlist = list; orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; utils.setParam("ShoppingCart","'"+JSON.stringify(jsonstr)); }};
使用方法:
<script>$(function(){var product = { 'id': 1, //属性名用引号括起来,属性间由逗号隔开 'name': '商品1', 'num':1, 'price':1};cart.addProduct(product); //商品加入到购物车 var productlist=cart.getProductList(); //取出购物车商品console.log(productlist);var isExist=cart.existProduct();console.log(isExist);cart.deleteProduct(1);var isExist1=cart.existProduct();console.log(isExist1);})</script>
阅读全文
1 0
- localStorage实现购物车
- 购物车的实现localstorage和sessionstorage
- 基于localstorage实现购物车功能
- h5-localstorage-购物车
- 基于html5 localStorage的购物车JS脚本
- localStorage存值取值以及存取JSON,以及基于html5 localStorage的购物车
- localStorage存值取值以及存取JSON,以及基于html5 localStorage的购物车
- localStorage存值取值以及存取JSON,以及基于html5 localStorage的购物车
- 购物车的实现
- JSP 购物车实现
- profile实现购物车
- Ajax实现购物车
- HashTable实现购物车
- session实现购物车
- 购物车实现
- 购物车实现原理
- 实现购物车
- 购物车的实现
- 阿里云大数据利器Maxcompute学习之-假如你使用过hive
- 关于LayoutInflater.inflate()的参数问题
- Android源码(4) --- 系统 Mainfest 文件解析流程
- 浅谈C语言中变量、常量、数组、字符串、指针的地址
- Fibre Channel光纤通道系统基础(二)
- localStorage实现购物车
- Echarts结合Ajax的动态图表(1)
- 566. Reshape the Matrix
- 无线通信:保证传输质量的三种技术
- Python lambda介绍
- VC6 MFC如何打开用户在编辑框内输入的文件?
- 解决移动端字体大小与设定大小不同的问题
- RecyclerView滑动距离计算 包含多个Header的万能开挂情况
- hdu1465 不容易系列之一 错排 递推