一只臭狮子 2015-07-20 14:53 采纳率: 50%
浏览 2328
已采纳

cookie购物车重复点击商品如何累加

商品列表:
商品abcd

点击购买a后
购物车:
初始a数量为1

返回商品列表
当再次点击a购买
a的数量为2

  • 写回答

4条回答 默认 最新

  • danielinbiti 2015-07-20 15:10
    关注
     <!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>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?