Home > Web Front-end > JS Tutorial > jQuery cookie-based shopping cart example analysis_jquery

jQuery cookie-based shopping cart example analysis_jquery

WBOY
Release: 2016-05-16 15:23:46
Original
1348 people have browsed it

The example in this article describes the shopping cart implemented by jQuery based on cookies. Share it with everyone for your reference, the details are as follows:

Here is an analysis of the jquery shopping cart principle, including adding products and quantities to the shopping cart cookie, determining whether there are products in the shopping cart, and if so, converting the json string into an object and returning the total number of current products in the cookie. .

Add items to shopping cart:

$(function(){
$(".tc").hide();
var PId = $("#hfPId").val(); // 商品的ID
var PName = $("#lblPName").text(); // 商品名称
var PMemberPrice = $("#lblPMemberPrice").text(); // 会员价
var PAmount = 1;
var jsonStr = "[{'PId':'" + PId + "','PName':'" + PName + "','PMemberPrice':'" + PMemberPrice + "','PAmount':'" + PAmount + "'}]";
//将商品放入购物车
$("#putCart").click(function(){
setCookie(PId, jsonStr);
});

Copy after login

Assignment:

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 * 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' : '';

Copy after login

I hope this article will be helpful to everyone in jQuery programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template