商品列表:
商品abcd
点击购买a后
购物车:
初始a数量为1
返回商品列表
当再次点击a购买
a的数量为2
商品列表:
商品abcd
点击购买a后
购物车:
初始a数量为1
返回商品列表
当再次点击a购买
a的数量为2
<!DOCTYPE html>
<html>
<body>
<script>
function addCookie(name,value){
document.cookie = name+"="+value;
}
function updateCookie(name,value){
var arrStr = document.cookie.split("; ");
for(var i = 0;i < arrStr.length;i ++){
var temp = arrStr[i].split("=");
if(temp[0] == name) {
temp[1] = parseInt(temp[1])+value;
addCookie(temp[0] ,temp[1]);
break;
}
}
}
function printCookie(){
var html = "";
var arrStr = document.cookie.split("; ");
for(var i = 0;i < arrStr.length;i ++){
var temp = arrStr[i].split("=");
html = temp[0] + "="+temp[1]+","+html;
}
document.getElementById('divid1').innerHTML = html;
}
</script>
<input type='button' value='添加A' onclick='addCookie("a",1);'/>
<input type='button' value='加1' onclick='updateCookie("a",1);'/>
<input type='button' value='打印' onclick='printCookie();'/>
<div id='divid1'>1111</div>
</body>
</html>