首頁  >  文章  >  web前端  >  jQuery基於cookie實現的購物車實例分析_jquery

jQuery基於cookie實現的購物車實例分析_jquery

WBOY
WBOY原創
2016-05-16 15:23:461275瀏覽

本文實例講述了jQuery基於cookie實現的購物車。分享給大家參考,具體如下:

這裡分析了jquery購物車原理,包括添加商品及數量到購物車cookie 中,判斷購物車中有無商品,如果有,則把json字符串轉換成對象,返回當前商品在cookie 中的總數。

將商品放入購物車:

$(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);
});

賦值:

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

希望本文所述對大家jQuery程式設計有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn