Client shopping cart operation instance implemented by jquery. cookie,

Source: Internet
Author: User

Client shopping cart operation instance implemented by jquery. cookie,

This document describes the client-side shopping cart operations implemented by jquery. cookie. We will share this with you for your reference. The details are as follows:

// Cart var Cart = function () {this. count = 0; this. total = 0; this. items = new Array () ;}; // var CartItem = function () {this. id = 0; this. name = ""; this. count = 0; this. price = 0 ;}; // var CartHelper = function () {this. cookieName = "yxhCart"; this. clear = function () {var cart = new Cart (); this. save (cart); return cart;}; // Add this to the cart. add = function (id, name, count, price) {var c Art = this. read (); var index = this. find (id); // if the ID already exists, overwrite the quantity if (index>-1) {cart. total-= (cart. items [index]. count * 100) * (cart. items [index]. price * 100)/10000); cart. items [index]. count = count; cart. total + = (cart. items [index]. count * 100) * (cart. items [index]. price * 100)/10000);} else {var item = new CartItem (); item. id = id; item. name = name; item. count = count; item. price = p Rice; cart. items. push (item); cart. count ++; cart. total + = (cart. items [index]. count * 100) * (cart. items [index]. price * 100)/10000);} this. save (cart); return cart;}; // change the quantity this. change = function (id, count) {var cart = this. read (); var index = this. find (id); cart. items [index]. count = count; this. save (cart); return cart;}; // remove from cart this. del = function (id) {var cart = this. read (); var index = This. find (id); if (index>-1) {var item = cart. items [index]; cart. count --; cart. total = cart. total-(item. count * 100) * (item. price * 100)/10000); cart. items. splice (index, 1); this. save (cart) ;}return cart ;}; // find this. find = function (id) {var cart = this. read (); var index =-1; for (var I = 0; I <cart. items. length; I ++) {if (cart. items [I]. id = id) {index = I;} return in Dex ;}; // COOKIE operation this. save = function (cart) {var source = ""; for (var I = 0; I <cart. items. length; I ++) {if (source! = "") {Source + = "| $ |";} source + = this. itemToString (cart. items [I]);} $. cookie (this. cookieName, source) ;}; this. read = function () {// Read the var source set in the COOKIE =$. cookie (this. cookieName); var cart = new Cart (); if (source = null | source = "") {return cart;} var arr = source. split ("| $ |"); cart. count = arr. length; for (var I = 0; I <arr. length; I ++) {var item = this. itemToObject (arr [I]); cart. items. push (item); cart. total + = (item. count * 100) * (item. price * 100)/10000);} return cart;}; this. itemToString = function (item) {return item. id + "|" + escape (item. name) + "|" + item. count + "|" + item. price;}; this. itemToObject = function (str) {var arr = str. split ('|'); var item = new CartItem (); item. id = arr [0]; item. name = unescape (arr [1]); item. count = arr [2]; item. price = arr [3]; return item ;};};

I hope this article will help you with jQuery programming.

Articles you may be interested in:
  • Analysis of jquery read/write cookie operation instances
  • JQuery cookie-based shopping cart instance analysis
  • Examples of cookie plug-in usage in jQuery
  • JQuery + Cookie-based online examination countdown to prevent refresh
  • JQuery implements a summary of mandatory cookie expiration Methods
  • How does jQuery control the cookie expiration time?
  • Jquery. cookie. js User Guide
  • JQuery cookie method example tutorial
  • Examples of jquery. cookie. js usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.