@{
ViewBag.Title = "Index1";
}
ViewBag.Title = "Index1";
}
<h2>Index1</h2>
<input id="Button1" type="button" value="加入" onclick="save()" />
<input id="Button1" type="button" value="显示" onclick="load()" />
<input id="Button1" type="button" value="显示" onclick="load()" />
<div id="div"></div>
<script>
if (getCookie("AAA") == null) {
setCookie("AAA", "[]");
}
if (getCookie("AAA") == null) {
setCookie("AAA", "[]");
}
//添加 保存数据
function save() {
//获取数据
var x = { Name: '电视', Price: 1800, desc: 'dkjajglkd', count: 1 };
//从AAA中获取字符串
var liststr = getCookie('AAA');
//将字符串强转为json格式
var list = JSON.parse(liststr);
//将数据添加list中
list.push(x);
//将json格式的list强转为字符串 并赋值到AAA中
var t = setCookie("AAA", JSON.stringify(list));
location.href = '/Home/Index2';
}
function save() {
//获取数据
var x = { Name: '电视', Price: 1800, desc: 'dkjajglkd', count: 1 };
//从AAA中获取字符串
var liststr = getCookie('AAA');
//将字符串强转为json格式
var list = JSON.parse(liststr);
//将数据添加list中
list.push(x);
//将json格式的list强转为字符串 并赋值到AAA中
var t = setCookie("AAA", JSON.stringify(list));
location.href = '/Home/Index2';
}
//显示
function load() {
//从AAA中获取数据
var liststr = getCookie('AAA');
var list = JSON.parse(liststr);
var x = "";
$(list).each(function () {
x += this.Name + ":" + this.Price + ":" + this.desc + "<br>";
});
//存入div显示
$("#div").html(x);
}
function load() {
//从AAA中获取数据
var liststr = getCookie('AAA');
var list = JSON.parse(liststr);
var x = "";
$(list).each(function () {
x += this.Name + ":" + this.Price + ":" + this.desc + "<br>";
});
//存入div显示
$("#div").html(x);
}
/**
* 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;
}
};
</script>