jQuery基于cookie實現(xiàn)的購物車實例分析
更新時間:2015年12月24日 12:06:26 作者:desteps
這篇文章主要介紹了jQuery基于cookie實現(xiàn)購物車的方法,結合實例形式簡單分析了jQuery基于cookie實現(xiàn)針對商品信息的購物車存儲功能,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了jQuery基于cookie實現(xiàn)的購物車。分享給大家供大家參考,具體如下:
這里分析了jquery購物車原理,包括添加商品及數(shù)量到購物車 cookie 中,判斷購物車中有無商品,如果有,則把json字符串轉換成對象,返回當前商品在 cookie 中的總數(shù)。
將商品放入購物車:
$(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程序設計有所幫助。
您可能感興趣的文章:
相關文章
JavaScript實現(xiàn)的彈出遮罩層特效經(jīng)典示例【基于jQuery】
這篇文章主要介紹了JavaScript實現(xiàn)的彈出遮罩層特效,結合實例形式分析了基于jQuery實現(xiàn)的頁面元素與屬性動態(tài)操作相關使用技巧,需要的朋友可以參考下2019-07-07學習從實踐開始之jQuery插件開發(fā) 對話框插件開發(fā)
之所以寫下這篇文章,是想將我的想法分享給大家;對于初學者,我希望他能從這篇文章中獲取對他有用的東西,對于經(jīng)驗豐富的開發(fā)者,我希望他能指出我的不足,給我更多的意見和建議;目的就是共同進步2012-04-04