js购物车

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="js/jquery.cookie.js"></script>
    <script type="text/javascript">
    $(function(){        
        $("a").bind("click",function(){            
            ckItem(this, 1, "+");
        });
        
    });
    function ckItem(e1, e2, e3)
    {
        var xh = $(e1).attr("pro");
        var strPros = "[";
        if($.cookie("pros") == null)
        {
            strPros += '{"proX":"' + xh + '","proS":"' + e2 + '"}';
        }
        else
        {
            var obj = eval("(" + $.cookie("pros") + ")");            
            var strPro = "";
            var flag = false;
            for(var i = 0;i < obj.length;i++)
            {                
                if(obj[i]["proX"] == xh)
                {
                    flag = true;
                    var sl = 0;
                    if(e3 == "+")
                    {
                        sl = parseInt(obj[i]["proS"]) + e2;
                    }
                    else if(e3 == "-")
                    {
                        sl = parseInt(obj[i]["proS"]) - e2;
                    }
                    else if(e3 == "*")
                    {
                        sl = parseInt(obj[i]["proS"]) * e2;
                    }
                    else if(e3 == "/")
                    {
                        sl = parseInt(obj[i]["proS"]) / e2;
                    }
                    else
                    {
                        sl = e2;
                    }                    
                    strPro += "," + '{"proX":"' + xh + '","proS":"' + sl + '"}';
                }
                else
                {
                    strPro += "," + '{"proX":"' + obj[i]["proX"] + '","proS":"' + obj[i]["proS"] + '"}';
                }
            }
            if(!flag)
            {
                strPro += "," + '{"proX":"' + xh + '","proS":"' + e2 + '"}';
            }
            var strProL = strPro.length;
            var strTemp = "";
            if(strProL > 1)
            {
                strTemp =strPro.substring(1, strProL);
            }
            strPros += strTemp;
        }
        strPros += "]";
        $.cookie("pros", strPros);
        qk0();
        alert("加入购物车成功!");
    }
    function qk0()
    {        
        if($.cookie("pros") != null)
        {
            if($.cookie("pros") == "[]")
                $.cookie("pros", null);
            else
            {
                var strPros = "";
                var obj = eval("(" + $.cookie("pros") + ")");
                var strPro = "";
                for(var i = 0;i < obj.length;i++)
                {
                    if(parseInt(obj[i]["proS"]) > 0)
                    {
                        strPro += "," + '{"proX":"' + obj[i]["proX"] + '","proS":"' + obj[i]["proS"] + '"}';
                    }                    
                }
                if(strPro.length > 0)
                {
                    strPros = "[" + strPro.substring(1, strPro.length) + "]";
                }
                if(strPros.length > 2)
                {
                    $.cookie("pros", strPros);
                }
                else
                {
                    $.cookie("pros", null);
                }
            }            
        }        
    }
    function aa()
    {
        alert($.cookie("pros"));
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <a href='javascript:void(0);' pro='aa'>abc</a>
    <a href='javascript:void(0);' pro='bb'>def</a>
    <a href='javascript:void(0);' pro='cc'>ghi</a>
    <input type="button" onclick="aa();" value="获得" />
    </form>
</body>
</html>
原文地址:https://www.cnblogs.com/daixingqing/p/2768388.html