JQuery cookie-based shopping cart instance analysis _ jquery

Source: Internet
Author: User
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.

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.