This example describes the way jquery implements a shopping cart based on JSON and cookies. Share to everyone for your reference, specific as follows:
JSON format:
[{' ProductID ': ABC ', ' num ': ' 1 '},{' ProductID ':D ef, ' Num ': ' 2 '}]
This plugin is used here in $.cookie. The code for this plugin is at the end of the article
* Add the goods and quantity to the cart cookie, return the total number of the current item in the cookie * * function addtoshoppingcar (ID, num) {var _num = 1;
if (num!= undefined) _num = num; var totalnum = _num; Total defaults to incoming parameter var Cookieset = {expires:7, path: '/'}; Set the cookie Path//$.cookie (Cookieproductid, NULL, cookieset);/clear cookie var jsonstr = "[{' ProductID ': '" + ID + "', ' Num ': "+ _num +" '}] "; Constructs a JSON string with the ID of commodity ID num is the quantity of this product if ($.cookie (cookieproductid) = = null) {$.cookie (Cookieproductid, Jsonstr, CookieS ET); If you do not have this cookie, set him} else {var jsonobj = eval (' (' + $.cookie (cookieproductid) + ') ');//If there is, convert the JSON string to object Var Findproduct = false;//Whether to find the product ID, true if found, otherwise falsh for (Var obj in jsonobj) {if (jsonobj[obj). ProductID = = ID) {Jsonobj[obj]. Num = number (Jsonobj[obj].
Num) + _num; Totalnum = Jsonobj[obj].
Num;
Findproduct = true;
Break
} if (Findproduct = False) {//not found, add jsonobj[jsonobj.length] = new Object (); Jsonobj[jsonobj.length -1].
ProductID = ID; JSONOBJ[JSONOBJ.LENGTH-1].
num = num; } $.cookie (Cookieproductid, Json.stringify (jsonobj), cookieset);
Write Coockie JSON requires json2.js support} return totalnum;
Alert ($.cookie (Cookieproductid));
}
The following is a cookie plug-in code Jquery.cookie = function (name, value, options) {if (typeof value!= ' undefined ') {//name and value g Iven, set cookie options = 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 (); Use expires attribute, Max-age isn't supported by IE} var path = Options.path? ';
Path= ' + options.path: '; var domain = Options.domain? ';
Domain= ' + options.domain: '; var secure = options.secure? ';
Secure ': ';
Document.cookie = [name, ' = ', encodeURIComponent (value), expires, path, domain, Secure].join ('); } else {//only name given, get cookie var cookievalue = null;
if (document.cookie && document.cookie!= ') {var cookies = Document.cookie.split (';');
for (var i = 0; i < cookies.length i++) {var cookie = Jquery.trim (Cookies[i]);
Does this cookie string begin with the name we want? if (cookie.substring (0, name.length + 1) = = (name + ' = ')) {cookievalue = decodeURIComponent (cookie.substring (NA
Me.length + 1));
Break
}} return cookievalue;
}
};
More interested readers of jquery-related content can view this site: "JQuery Cookie Tips Summary", "jquery table (table) Operation Tips Summary", "jquery drag-and-drop effects and tips summary", "jquery extension Techniques Summary", " jquery Common Classic Effects Summary "jquery animation and special effects usage Summary", "jquery selector usage Summary" and "jquery common Plug-ins and Usage summary"
I hope this article will help you with the jquery program design.