//商品列表var id=@Session["Id"]; function load() { $.ajax({ url: "http://localhost:52975/api/Goods/GetGood/"+id, type: "post", dataType: "json", success: function (d) { $("#name").text(d.Name); $("#price").text("¥"+d.Price+"元"); } }) } load();if (getCookie("shopcar")==null) { setCookie("shopcar", "[]"); } function add() {var x = { Name: $("#name").text(), Price:$("#price").text() };var liststr = getCookie('shopcar');var list = JSON.parse(liststr); list.push(x); setCookie("shopcar", JSON.stringify(list)); location.href = '/Goods/ShopCar'; }/** * cookie中存值 * */function setCookie(name, value) {if (value) {var days = 1; //定义一天var exp = new Date(); exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);// 写入Cookie, toGMTString将时间转换成字符串document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString; } };/** * cookie中取值 * */function getCookie(name) {var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段if (arr = document.cookie.match(reg)) {return unescape(arr[2]); } else {return null; } }; //购物车function load() {var liststr = getCookie("shopcar");var list = JSON.parse(liststr); $("#tb").empty(); $(list).each(function () { $("#tb").append('' +'' + this.Name + '' +'' + this.Price + '' +'') }) } load();