http://blog.csdn.net/wangqiuyun/article/details/8435649
Recently in doing HTML5 this piece, refer to the online code to write a shopping cart JS script, very simple, directly on the code, Shoppingcart.js:
[JavaScript]View PlainCopy
- Utils = {
- SetParam: function (name,value) {
- Localstorage.setitem (Name,value)
- },
- GetParam: function (name) {
- return Localstorage.getitem (name)
- }
- }
- product={
- id:0,
- Name: "",
- num:0,
- price:0.00,
- };
- orderdetail={
- Username: "",
- Phone: "",
- Address: "",
- ZipCode: "",
- totalnumber:0,
- totalamount:0.00
- }
- Cart = {
- //Add items to your shopping cart
- Addproduct:function (product) {
- var ShoppingCart = Utils.getparam ("ShoppingCart");
- if (shoppingcart==null| | shoppingcart=="") {
- //First time to add products
- var jsonstr = {"productlist": [{"id":p roduct.id,"name":p roduct.name,"num":p roduct.num," Price ":p Roduct.price}]," Totalnumber ":p roduct.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;
- //Find out if the item is in the cart
- 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) {
- //Without the product, add it directly
- Productlist.push ({"id":p roduct.id,"name":p roduct.name,"num":p roduct.num,"Price":p Roduct.price} );
- }
- //Recalculate Total 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;
- //Save Shopping Cart
- Utils.setparam ("ShoppingCart","'" +json.stringify (JSONSTR));
- }
- },
- //Modify the quantity of goods to buy
- 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;
- }
- }
- },
- //Get all items in the shopping cart
- 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;
- },
- //Determine if a product exists in the shopping cart
- Existproduct:function (ID) {
- var ShoppingCart = Utils.getparam ("ShoppingCart");
- 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) {
- result = true;
- }
- }
- return result;
- },
- //delete items in cart
- Deleteproduct:function (ID) {
- var ShoppingCart = Utils.getparam ("ShoppingCart");
- var jsonstr = Json.parse (Shoppingcart.substr (1,shoppingcart.length));
- var productlist = jsonstr.productlist;
- var list=[];
- 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));
- }
- };
It's also easy to use:
[JavaScript]View PlainCopy
- var product =
- {
- ' ID ': ID, //attribute name enclosed in quotation marks, separated by commas between attributes
- ' name ': ' hhh ',
- ' num ': JQ (' #text-4 '). Val (),
- ' price ': 199.9
- };
- Add items to your shopping cart
- Cart.addproduct (product);
- var productlist=cart.getproductlist (); //Remove shopping cart items
- Alert (', ' product: ' +productlist[0].id+ '+productlist[0].name+ '+productlist[0].num+ '+ Productlist[0].price, ' OK ');
Shopping Cart JS script based on HTML5 Localstorage