JS+COOKIE购物车

来源:互联网 发布:分布估计算法代码 编辑:程序博客网 时间:2024/04/29 13:35
 

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

<!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">

    </script>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td>
                <asp:Image ID="Image1" runat="server" Height="226px" ImageUrl="~/001.jpg"
                    Width="446px" />
            </td>
            <td>
                <asp:Image ID="Image2" runat="server" Height="226px"
                    ImageUrl="~/766761_111649063996_2.jpg" Width="446px" />
            </td>
        </tr>
        <tr>
            <td style="text-align: center">
                <a href="buy.aspx?id=产品1" style="text-align: center">去购物</a></td>
            <td style="text-align: center">
                <a href="buy.aspx?id=产品2">去购物</a></td>
        </tr>
    </table>
    </form>
</body>
</html>

 

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

<!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>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 514px;
        }
    </style>
    <script type="text/javascript">

//        function seekcart() {
//            if (document.cookie.length > 0) {
//                var name = document.getElementById("disp").innerHTML;
//                if (document.cookie.search(name) >= 0) { //判断是否记录存在
//                    var cook = document.cookie;
//                    var product = cook.split('|');   //分成单记录
//                    var newproduct = "";             //修改总记录
//                    var repari = "";                 //修改后的数量
//                    var repairrecord = "";
//                    for (var item in product) {
//                        repairrecord=product[item];         //记录当前的记录原始值
//                        productname = product[item].substring(0, product[item].indexOf(','));        //品名
//                        if (productname == name) {
//                            productprice = product[item].substring(product[item].indexOf(',') + 1);  //数量
//                            repairrecord = productname + ',' + (parseInt(productprice)+1);
//                        }

//                        if (newproduct.substr(newproduct.length-1,1)=='|') {
//                            newproduct += repairrecord;
//                        } else {
//                            newproduct += repairrecord + '|';
//                        }
//                                                                                 //串回去
//                    }
//                    var thedate = new Date();
//                    thedate.setDate(thedate.getDate() + 1);
//                    document.cookie = newproduct + ';Expires=' + thedate.toGMTString()

//                } else {
//                    //无此类商品
//                   alert("无此类");
//                    var thedate = new Date();
//                    thedate.setDate(thedate.getDate() + 1);
//                    var oldcookie = document.cookie;
//                    document.cookie = oldcookie +'|'+name+',1'+ ';Expires=' + thedate.toGMTString();
//                }
//            } else {
//                //写入第一次的
//               alert("第一次");
//                var name = document.getElementById("disp").innerHTML;
//                var thedate = new Date();
//                thedate.setDate(thedate.getDate() + 1);
//                var num = new Array();
//                num[0] = name;
//                num[1] = 1;
//                var old = document.cookie + num;
//                document.cookie = old + ';Expires=' + thedate.toGMTString();
//            }

//        }
  
        function seekcart() {
            if (document.cookie.length > 0) {
                var name = document.getElementById("disp").innerHTML;
                if (document.cookie.search(name) >= 0) {       //判断是否记录存在
                    var cook = document.cookie;
                    var product = cook.split('|');             //分成单记录
                    var newproduct = "";                       //修改总记录
                    var repari = "";                           //修改后的数量
                    var repairrecord = "";
                    for (var item in product) {
                        repairrecord = product[item];         //记录当前的记录原始值
                        productname = product[item].substring(0, product[item].indexOf(','));        //品名
                        if (productname == name) {
                            productprice = product[item].substring(product[item].indexOf(',') + 1);  //数量
                            repari = productname + ',' + (parseInt(productprice) + 1);
                            alert("'" + product[item].toString() + "'" + "=" + repari);
                            var s=cook.replace(repairrecord, repari);
                            cook = s;
                        }
                      //  alert(s);
                       // alert(repairrecord);

                        //  cook.replace(123, 456);
                     
//                        if (newproduct.substr(newproduct.length,1) == '|') {
//                            newproduct += repairrecord;
//                        } else {
//                            newproduct += repairrecord + '|';
//                                                }
 
                        //串回去
                    }
                    var thedate = new Date();
                    thedate.setDate(thedate.getDate() + 1);
                    document.cookie = cook + ';Expires=' + thedate.toGMTString()

                } else {
                    //无此类商品
                    alert("无此类");
                    var thedate = new Date();
                    thedate.setDate(thedate.getDate() + 1);
                    var oldcookie = document.cookie;
                    document.cookie = oldcookie + '|' + name + ',1' + ';Expires=' + thedate.toGMTString();
                }
            } else {
                //写入第一次的
                alert("第一次");
                var name = document.getElementById("disp").innerHTML;
                var thedate = new Date();
                thedate.setDate(thedate.getDate() + 1);
                var num = new Array();
                num[0] = name;
                num[1] = 1;
                var old = document.cookie + num;
                document.cookie = old + ';Expires=' + thedate.toGMTString();
            }
           // document.write(document.cookie);
        } 

 
 

function Button2_onclick() {
 
          document.write(document.cookie);
   
}

function Button4_onclick() {
    if (document.cookie.length > 0) {
        var name = document.getElementById("disp").innerHTML;
        if (document.cookie.search(name) >= 0) {       //判断是否记录存在
            var cook = document.cookie;
            var product = cook.split('|');             //分成单记录
            var newproduct = "";                       //修改总记录
            var repari = "";                           //修改后的数量
            var repairrecord = "";
            for (var item in product) {
                repairrecord = product[item];         //记录当前的记录原始值
                productname = product[item].substring(0, product[item].indexOf(','));        //品名
                if (productname == name) {
                    productprice = product[item].substring(product[item].indexOf(',') + 1);  //数量
                    repari = productname + ',' + (parseInt(productprice) + 1);
                    alert("'" + product[item].toString() + "'" + "=" + repari);
                    var s = cook.replace(repairrecord, repari);
                    cook = s;
                }
              //  alert(s);
                // alert(repairrecord);

                //  cook.replace(123, 456);

                //                        if (newproduct.substr(newproduct.length,1) == '|') {
                //                            newproduct += repairrecord;
                //                        } else {
                //                            newproduct += repairrecord + '|';
                //                                                }

                //串回去
            }
            var thedate = new Date();
            thedate.setDate(thedate.getDate() + 1);
            document.cookie = cook + ';Expires=' + thedate.toGMTString()

        } else {
            //无此类商品
            alert("无此类");
            var thedate = new Date();
            thedate.setDate(thedate.getDate() + 1);
            var oldcookie = document.cookie;
            document.cookie = oldcookie + '|' + name + ',1' + ';Expires=' + thedate.toGMTString();
        }
    } else {
        //写入第一次的
        alert("第一次");
        var name = document.getElementById("disp").innerHTML;
        var thedate = new Date();
        thedate.setDate(thedate.getDate() + 1);
        var num = new Array();
        num[0] = name;
        num[1] = 1;
        var old = document.cookie + num;
        document.cookie = old + ';Expires=' + thedate.toGMTString();
    }
  //  document.write(document.cookie);
}

function Button5_onclick() {
    if (document.cookie.length > 0) {
        var name = document.getElementById("disp").innerHTML;
        if (document.cookie.search(name) >= 0) {       //判断是否记录存在
            var cook = document.cookie;
            var product = cook.split('|');             //分成单记录
            var newproduct = "";                       //修改总记录
            var repari = "";                           //修改后的数量
            var repairrecord = "";
            for (var item in product) {
                repairrecord = product[item];         //记录当前的记录原始值
                productname = product[item].substring(0, product[item].indexOf(','));        //品名
                if (productname == name) {
                    productprice = product[item].substring(product[item].indexOf(',') + 1);  //数量
                    repari = productname + ',' + (parseInt(productprice) - 1);
                    alert("'" + product[item].toString() + "'" + "=" + repari);
                    var s = cook.replace(repairrecord, repari);
                    cook = s;
                }
                //  alert(s);
                // alert(repairrecord);

                //  cook.replace(123, 456);

                //                        if (newproduct.substr(newproduct.length,1) == '|') {
                //                            newproduct += repairrecord;
                //                        } else {
                //                            newproduct += repairrecord + '|';
                //                                                }

                //串回去
            }
            var thedate = new Date();
            thedate.setDate(thedate.getDate() + 1);
            document.cookie = cook + ';Expires=' + thedate.toGMTString()

        } else {
            //无此类商品
            alert("无此类");
            var thedate = new Date();
            thedate.setDate(thedate.getDate() + 1);
            var oldcookie = document.cookie;
            document.cookie = oldcookie + '|' + name + ',1' + ';Expires=' + thedate.toGMTString();
        }
    } else {
        //写入第一次的
        alert("第一次");
        var name = document.getElementById("disp").innerHTML;
        var thedate = new Date();
        thedate.setDate(thedate.getDate() + 1);
        var num = new Array();
        num[0] = name;
        num[1] = 1;
        var old = document.cookie + num;
        document.cookie = old + ';Expires=' + thedate.toGMTString();
    }
}

    </script>
  
</head>
<body>
    <form id="form1" runat="server">
    <table class="style1">
        <tr>
            <td class="style2">
                <img alt="" runat="server" id="img" src="" style="height: 207px; width: 511px" />
                <div runat="server" id="disp" style="height: 13px; width: 143px">
                </div>
            </td>
            <td>
                件数:<input id="Text1" type="text" /><input id="Button3" type="button" value="购物" onclick="return Button3_onclick()" /></td>
        </tr>
        <tr>
            <td class="style2">
                <input id="Button1" type="button" value="购物" onclick="seekcart()" onclick="seekcart()" /><input
                    id="Button4" type="button" value="+" onclick="return Button4_onclick()" /><input id="Button5" type="button"
                    value="-" onclick="return Button5_onclick()" /></td>
            <td>
                <input id="Button2" type="button" value="查看" onclick="return Button2_onclick()" /></td>
        </tr>
    </table>
    </form>
</body>
</html>