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); });
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' : '';
I hope this article will be helpful to everyone in jQuery programming.