//商品列表 var id=@Session["Id"]; function load() { $.ajax({ url: "http://localhost:52975/api/Goods/GetGood/"+id, type: "post", dataType: "json", success: ...
//商品列表
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(
'<tr>' +
'<td>' + this.Name + '</td>' +
'<td>' + this.Price + '</td>' +
'</tr>'
)
})
}
load();
作者還在學習中,發現錯誤的請在評論區留言。 如果有客友覺得文章還行的話,請點波推薦哦