This article mainly introduces jQuery's method of implementing Shopping Cart Based on cookies, and analyzes jQuery's function of storing Shopping Cart Based on cookies based on examples, which has some reference value, for more information, see the example in this article. We will share this with you for your reference. The details are as follows:
This section analyzes the jquery shopping cart principle, including adding items and quantity to the shopping cart cookie to determine whether there are any items in the shopping cart. If yes, the json string is converted into an object, returns the total number of items in the current cookie.
Put the item in the shopping cart:
$ (Function () {$ (". tc "). hide (); var PId =$ ("# hfPId "). val (); // the product's IDvar PName = $ ("# lblPName "). text (); // product name var PMemberPrice = $ ("# lblPMemberPrice "). text (); // member price var PAmount = 1; var jsonStr = "[{'pid ':'" + PId + "', 'pname ': '"+ PName +"', 'pmemberprice': '"+ PMemberPrice +"', 'pamount ':' "+ PAmount +" '}] "; // Add the item to the shopping cart $ ("# putCart "). click (function () {setCookie (PId, jsonStr );});
Assignment:
var setCookie = function(name, value, options){if (typeof value != 'undefined') { // name and value given, set cookieoptions = options || {};if (value === null) {value = '';options.expires = -1;}var expires = '';if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {var date;if (typeof options.expires == 'number') {date = new Date();date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));} else {date = options.expires;}expires = '; expires=' + date.toUTCString();}var path = options.path ? '; path=' + (options.path) : '';var domain = options.domain ? '; domain=' + (options.domain) : '';var secure = options.secure ? '; secure' : '';
I hope this article will help you with jQuery programming.