EF getCookie

table class="table table-bordered">
<thead>
<tr>
<td>商品名称</td>
<td>商品图片</td>
<td>商品价格</td>
<td>加入购物车</td>
</tr>
</thead>
<tbody id="tb"></tbody>
</table>

<table class="table table-bordered">
<tr>
<td><input id="Button1" type="button" value="首页" onclick="first()" /></td>
<td><input id="Button1" type="button" value="上一页" onclick="prve()" /></td>
<td><input id="Button1" type="button" value="下一页" onclick="next()" /></td>
<td><input id="Button1" type="button" value="尾页" onclick="last()" /></td>
</tr>
</table>

<script>
var index1 = 0;
var pagecount = 0;
function load(index) {
index1 = index;
$.ajax({
url: "http://localhost:50825/api/Shopping/GetGoods2",
data: { index: index1, size: 2 },
type: "get",
dataType: "json",
success:
function (d) {
$("#tb").empty();
$(d.List).each(function () {
$("#tb").append(
'<tr>' +
'<td>' + this.Name + '</td>' +
'<td><img src="http://localhost:50825' + this.Img + '" width="80" height="60" /></td>' +
'<td>' + this.Price + '</td>' +
'<td><input id="Button1" type="button" value="加入购物车" onclick="addcar()" /></td>' +
'</tr>'
)
})
pagecount = d.PageCount;
}
})
}
load(1);

function first() {
index1 = 1;
load(index1);
}
function prve() {
index1--;
if (index1 == 0) {
index1 = 1;
}
load(index1);
}
function next() {
index1++;
if (index1 > pagecount) {
index1 = pagecount;
}
load(index1);
}
function last() {
load(pagecount);
}

//cookie零时存购物车
function addcar() {
//判断cookie是否有值
if (getCookie("shopcar") == null) {
//数组类型
setCookie("shopcar", "[]");
}
//这里举个例子,将值写死
var obj = {
Name: "球鞋",
Price: "1200"
};

//获取值,此时为字符串类型
var liststr = getCookie('shopcar');
//类型转换
var list = JSON.parse(liststr);
//追加值
list.push(obj);
//保存到cookie中
setCookie("shopcar", JSON.stringify(list));
location.href = "/Default/ShopCar";
}

//取值
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;
}
};
//存值
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段
if (arr = document.cookie.match(reg)) {
return unescape(arr[2]);
} else {
return null;
}
};
</script>

原文地址:https://www.cnblogs.com/CoreColor/p/13448862.html