最近在做html5这一块,参考网上的代码写了一个购物车js脚本,很简单,直接上代码,shoppingcart.js:
[javascript]
utils = {
tparam : function (name,value){
localstorage.titem(name,value)
},
getparam : function(name){
return localstorage.getitem(name)
}
}
product={
id:0,
name:””,
num:0,
price:0.00,
};
orderdetail={
urname:””,
phone:””,
address:””,
zipcode:””,
totalnumber:0,
totalamount:0.00
}
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.tparam(“shoppingcart”,”‘”+json.stringify(jsonstr));
}el{
var jsonstr = json.par(shoppingcart.substr(1,shoppingcart.le干部教育培训ngth));
var productlist = jsonstr.productlist;
var result=fal;
//查找购物车中是否有该商品
for(var i in productlist){
if(productlist[i].id==product.id){
productlist[i].num=parint(productlist[i].num)+parint(product.num);
result = true;
}
}
if(!result){
//没有该商品就直接加进去
productlist.push({“id”:product.id,”name”:product.name,”num”:product.num,”price”:product.price});
}
//重新计算总价
jsonstr.totalnumber=parint(jsonstr.totalnumber)+parint(product.num);
jsonstr.totalamount=parfloat(jsonstr.totalamount)+(parint(product.num)*parfloat(product.price));
orderdetail.totalnumber = jsonstr.totalnumber;
orderdetail.totalamount = jsonstr.totalamount;
//保存购物车
utils.tparam(“shoppingcart”,”‘”+json.stringify(jsonstr));
}
},
//修改给买商品数量
updateproductnum:function(id,num){
var shoppingcart = utils.getparam(“shoppingcart”);
var jsonstr = json.par(shoppingcart.substr(1,shoppingcart.length));
var productlist = jsonstr.productlist;
for(var i in productlist){
if(productlist[i].id==id){
jsonstr.totalnumber=parint(json含有雨的诗句str.totalnumhit过去式ber)+(parint(num)-parint(productlist[i].num));
jsonstr.totalamount=parfloat(jsonstr.totalamount)+((parint(num)*pars草皮efloat(productlist[i].price))-parint(productlist[i].num)*parfloat(productlist[i].price));
productlist[i].num=parint(num);
orderdetail.totalnumber = jsonstr.totalnumber;
orderdetail.totalamount = jsonstr.totalamount;
utils.tparam(“shoppingcart”,”‘”+json.stringify(jsonstr));
return;
}
}
},
//获取购物车中的所有商品
getproductlist:function(){
var shoppingcart = utils.getparam(“shoppingcart”);
var jsonstr = json.par(shoppingcart.substr(1,shoppingcart.length));
var productlist = jsonstr.productlist;
orderdetail.totalnumber = jsonstr.totalnumber;
orderdetail.totalamount = jsonstr.totalamount;
return productlist;
},
//判断购物车中是否存在商品
existproduct:function(id){
var shoppingcart = utils.getparam(“shoppingcart”);
var jsonstr = json.par(shoppingcart.substr(1,shoppingcart.length));
var productlist = jsonstr.productlist;
var result=fal;
for(var i in productlist){
if(productlist[i].id==product.id){
result = true;
}
}
return result;
},
//删除购物车中商品
deleteproduct:function(id){
var shop什么是多士炉pingcart = utils.getparam(“shoppingcart”);
var jsonstr = json.par(shoppingcart.substr(1,shoppingcart.length));
var productlist = jsonstr.productlist;
var list=[];
for(var i in productlist){
if(productlist[i].id==id){
jsonstr.totalnumber=parint(jsonstr.totalnumber)-parint(productlist[i].num);
jsonstr.totalamount=parfloat(jsonstr.totalamount)-parint(productlist[i].num)*parfloat(productlist[i].price);
}el{
list.push(productlist[i]);
}
}
jsonstr.productlist = list;
orderdetail.totalnumber = jsonstr.totalnumber;
orderdetail.totalamount = jsonstr.totalamount;
utils.tparam(“shoppingcart”,”‘”+json.stringify(jsonstr));
}
};
使用也很简单:
[javascript]
var product =
{
‘id’: id, //属性名用引号括起来,属性间由逗号隔开
‘name’: ‘hhh’,
‘num’:jq(‘#text-4’).val(),
‘price’:199.9
};
//商品加入到购物车
cart.addproduct(product);
var productlist=cart.getproductlist();//取出购物车商品
alert(”, ‘商品:’+productlist[0].id+’ ‘+productlist[0].name+’ ‘+productlist[0].num+’ ‘+productlist[0].price, ‘确定’);
本文发布于:2023-04-03 01:50:02,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/64698071f0d4017213640feb43546949.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:基于html5 localStorage的购物车JS脚本.doc
本文 PDF 下载地址:基于html5 localStorage的购物车JS脚本.pdf
留言与评论(共有 0 条评论) |