jquery based on cookie implementation of shopping cart instance analysis _jquery

Source: Internet
Author: User

The example in this article is about jquery's cookie-based shopping cart. Share to everyone for your reference, specific as follows:

This article analyzes the jquery shopping cart principles, including adding goods and quantities to the shopping cart cookie, determining if there are any items in the shopping cart, and if so, converting the JSON string into objects and returning the total number of the current item in the cookie.

Put the goods into the shopping cart:

$ (function () {
$ (". TC"). Hide ();
var PId = $ ("#hfPId"). Val (); Product ID
var pname = $ ("#lblPName"). Text ();//commodity name
var pmemberprice = $ ("#lblPMemberPrice"). Text ();//Member Price
V Ar pamount = 1;
var jsonstr = "[{' PID ': '" + pid + "', ' pname ': '" + pname + "', ' pmemberprice ': '" + Pmemberprice + "', ' pamount ': '" + pamount + "'}]";
Put the item in the shopping cart
$ ("#putCart"). Click (function () {
Setcookie (PId, jsonstr);
});

Assign value:

var Setcookie = function (name, value, options) {
if (typeof value!= ' undefined ') {//name and value given, 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 * * * * * 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 the jquery program design.

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.