java cookie实现购物车_jQuery基于cookie实现的购物车实例分析

本文实例讲述了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程序设计有所帮助。

package shoppingcart.biz; import java.util.HashMap; import java.util.Iterator; import java.util.Map; /** * 购物车 */ public class ShoppingCart { private Map cartMap = null; //保存Product的Map /** * 购物车构造函数 */ public ShoppingCart(){ cartMap = new HashMap(); } /** * 取得存放产品的cartMap */ public Map getCartMap(){ return this.cartMap; } /** * 向购物车中添加产品 */ public boolean addProduct(String productId){ if(existProduct(productId)){ // 产品已存在则增加数量 Product product = cartMap.get(productId); product.setProductNum(product.getProductNum() + 1); return true; } else{ // 否则新加入该产品 Product product = new Product(productId); if(product.getProductId()==null){ return false; // 数据库中无该产品 }else{ cartMap.put(productId, product); return false; } } } /** * 检查购物车里是否已存在该产品 */ public boolean existProduct(String productId){ Iterator hmkey = cartMap.keySet().iterator(); while(hmkey.hasNext()){ String pid = hmkey.next(); if(pid.equals(productId)){ return true; } } return false; } /** * 从购物车中移除指定产品 */ public void delProduct(String productId){ cartMap.remove(productId); } /** * 获取购物车产品数量 */ public int getProductNum(){ int productNum = 0; Iterator hmEntry = cartMap.values().iterator(); while(hmEntry.hasNext()){ productNum += hmEntry.next().getProductNum(); } return productNum; } /** * 统计购物车商品总价 */ public double getTotalprice(){ double totalPrice = 0.0; Iterator hmkey = cartMap.keySet().iterator(); Product product = null; while(hmkey.hasNext()){ product = cartMap.get(hmkey.next()); totalPrice += product.getProductPirce() * product.getProductNum(); } return totalPrice; } /** * 清空购物车 */ public void clearCart(){ cartMap.clear(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值