zoukankan      html  css  js  c++  java
  • 使用JQuery 实现类似购物商城的购物车

    使用JQuery  Clone 模板来实现商品信息的展示,展现形式可以通过修改模板中的td来确定每一行显示多少个商品信息

    商品信息使用JSON数据来模拟

    同一个产品点击多次,不会重复添加,而是在已有的基础上数量+1,

    商品数量也可以手动输入,当输入0时,该商品将自动从购物车删除(点击减号到小于1时,也会提示是否从购物车删除商品信息)

    每个产品的价格和总价都会根据添加和删除的操作来动态计算

    附下载链接:https://files.cnblogs.com/xffy1028/JQuery%E8%B4%AD%E7%89%A9%E8%BD%A61.rar

    基本的功能都已经实现, 建议使用IE6,7,8,浏览器运行,其他浏览器没有测试

    HTML代码:

    View Code
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head>
    <body>
    <h2 style="text-align:left">商品列表</h2>
    <table width="800" border="0" id="tblProduct" cellpadding="1" cellspacing="1" bgcolor="black">
    <tr id="trMaster" bgcolor="white" style="display:none">
    <td>
    编号:<span>000001</span><br />
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />
    <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
    </td>
    <td>
    编号:<span>000001</span><br />
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />
    <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
    </td>
    <td>
    编号:<span>000001</span><br />
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />
    <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
    </td>
    <td>
    编号:<span>000001</span><br />
    名称:<span>aa</span><br />
    价格:<span>2.5</span><br />
    描述:<span>aaaaa</span><br />
    <div style="text-align:right"><input type="button" name="btnBuy" value="buy" /></div>
    </td>

    </tr>
    </table>
    <h2 style="text-align:left">购物车</h2>
    <table width="800" border="0" id="tblOrder" cellpadding="1" cellspacing="1" bgcolor="black">
    <tr bgcolor="white"><td>序号</td><td>编号</td><td>名称</td><td>描述</td><td>数量</td><td>单价</td><td>总价</td><td>删除</td></tr>
    <tr id="orderMarter" bgcolor="white" style="display:none">
    <td style=" 5%">1</td>
    <td style=" 10%">000001</td>
    <td style=" 10%">aa</td>
    <td>aaaaa</td>
    <td style=" 15%">
    <input type="text" name="txtNum" style="50px;" value="0" onkeyup="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"/>
    <input type="button" name='btnAdd' style="15px; text-align:center" value="+" />
    <input type="button" name='btnCut' style="15px; text-align:center" value="-" />
    </td>
    <td style=" 10%">0</td>
    <td style=" 10%"><font color="red">0</font></td>
    <td style=" 5%; text-align:center"><input type="button" name='btnRemove' style="30px; text-align:center" value="X" /></td>
    </tr>
    </table>
    <table width="800" border="0" id="tblTotal" cellpadding="1" cellspacing="1" bgcolor="black">
    <tr bgcolor="white" align="right">
    <td>总价</td>
    <td id="tdTotal"><font color="#FF0000" size="+1" face="Arial, Helvetica, sans-serif">0.0</font></td>
    </tr>
    </table>

    </body>

    JS 代码

    View Code
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    /*
    @@
    @@
    @@
    */
    var product=[
    {prodId:"000001",prodName:"aa",price:2.5,description:"aaaaa"},
    {prodId:"000002",prodName:"bb",price:2.4,description:"bbbbb"},
    {prodId:"000003",prodName:"cc",price:2.3,description:"ccccc"},
    {prodId:"000004",prodName:"dd",price:2.2,description:"ddddd"},
    {prodId:"000005",prodName:"ee",price:2.1,description:"eeeee"},
    {prodId:"000006",prodName:"ff",price:2.0,description:"fffff"},
    {prodId:"000007",prodName:"gg",price:1.9,description:"ggggg"},
    {prodId:"000008",prodName:"hh",price:1.8,description:"hhhhh"},
    {prodId:"000009",prodName:"ii",price:1.7,description:"iiiii"}
    ];
    </script>


    <script type="text/javascript">
    var j=0,i=-1,k=-1,len=$("#trMaster td").size();//数据和模板的td 都可以自定义,格式正确即可
    $.each(product,function(index,prod){
    i++;
    k++;
    if(i%len==0){
    j++;
    CloneTR(j);//根据模板来克隆行
    }
    if(k==len)
    k=0;
    setTD(j,k,prod.prodId,prod.prodName,prod.price,prod.description);//给克隆行的td 赋值
    });
    //根据模板来克隆行
    function CloneTR(id){
    $("#trMaster").clone(true).css("display","block").attr("id","tr"+id).appendTo("#tblProduct");
    $("#tr"+id+" td span").empty();
    }
    //给克隆行的td 赋值
    function setTD(trId,index,prodId,prodName,price,description){
    var tr=$("#tr"+trId);
    $(tr).find("td:eq("+index+")").find("span:eq(0)").html(prodId);
    $(tr).find("td:eq("+index+")").find("span:eq(1)").html(prodName);
    $(tr).find("td:eq("+index+")").find("span:eq(2)").html(price);
    $(tr).find("td:eq("+index+")").find("span:eq(3)").html(description);
    }
    var tempId="";
    var num=0;
    //点击buy
    $("input[name='btnBuy']").click(function(){
    var elem=$(this).parent().parent();//获取点击的button 的td
    var prodId=$(elem).find("span:eq(0)").html();
    var prodName=$(elem).find("span:eq(1)").html();
    var price=$(elem).find("span:eq(2)").html();
    var description=$(elem).find("span:eq(3)").html();
    if(prodId==""||prodId==null||prodId==undefined){
    alert("请点击其他产品");
    }else{
    if(tempId.indexOf(prodId)!=-1){
    if(confirm('已存在,确定数量+1 吗?')){
    $("#tblOrder tr:gt(1)").each(function(){
    if($(this).find("td:eq(1)").html()==prodId){
    var num=$(this).find("td:eq(4)").find("input[name='txtNum']").attr("value");
    $(this).find("td:eq(4)").find("input[name='txtNum']").attr("value",+num+1);//相当于parseInt(num)+1;
    }
    });
    }
    }else{
    num++;
    CloneOrder(num,prodId,prodName,price,description);
    }
    tempId+=prodId+",";
    $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
    }
    });
    //根据订单模板来clone 订单
    function CloneOrder(id,prodId,prodName,price,description){
    $("#orderMarter").clone(true).css("display","block").attr("id","tro"+id).appendTo("#tblOrder");
    var tr=$("#tro"+id);
    $(tr).find("td:eq(0)").html(num);
    $(tr).find("td:eq(1)").html(prodId);
    $(tr).find("td:eq(2)").html(prodName);
    $(tr).find("td:eq(3)").html(description);
    $(tr).find("td:eq(4)").find("input[name='txtNum']").attr("value","1");
    $(tr).find("td:eq(5)").html(price);
    $(tr).find("td:eq(6)").html("<font color='red'>"+price+"</font>");
    }
    //获取总共价格
    function GetTotalPrice(){
    var totalNum=0;
    $("#tblOrder tr:gt(1)").each(function(){
    var value=parseFloat($(this).find("td:eq(6)").text());
    totalNum+=value;
    });
    return totalNum;
    }
    $(function(){
    $("#tblOrder input[name='txtNum']").bind("propertychange change blur",function(){
    var value=$(this).val();
    var tr=$(this).parent().parent();
    if(value==0){
    if(confirm('确定要删除该商品吗?')){
    var prodId=$(tr).find("td:eq(1)").html();
    tempId=tempId.replace(prodId+",","");
    $(tr).remove();
    }
    }else{
    var price=$(tr).find("td:eq(5)").html();
    var total=value*price;
    $(tr).find("td:eq(6)").html("<font color='red'>"+total.toFixed(2)+"</font>");
    $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
    }
    });
    //加1
    $("#tblOrder input[name='btnAdd']").click(function(){
    var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");
    var value=$(txtBox).attr("value");
    value=+value+1;
    $(txtBox).attr("value",value)

    });
    //减1
    $("#tblOrder input[name='btnCut']").click(function(){
    var txtBox=$(this).parent().parent().find("td:eq(4)").find("input[name='txtNum']");
    var tr=$(this).parent().parent();
    var value=$(txtBox).attr("value");
    if(value>1){
    value=+value-1;
    $(txtBox).attr("value",value)
    }else{
    if(confirm('确定要删除该商品吗?')){
    var prodId=$(tr).find("td:eq(1)").html();
    tempId=tempId.replace(prodId+",","");
    $(tr).remove();
    $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
    }
    }
    });

    //删除btnRemove
    $("#tblOrder input[name='btnRemove']").click(function(){
    var tr=$(this).parent().parent();
    if(confirm('确定要删除该商品吗?')){
    var prodId=$(tr).find("td:eq(1)").html();
    tempId=tempId.replace(prodId+",","");
    $(tr).remove();
    $("#tdTotal").html("<font color='#FF0000' size='+1' face='Arial, Helvetica, sans-serif'>"+GetTotalPrice().toFixed(2)+"</font>");
    }
    });
    });

    </script>

     http://s.click.taobao.com/t_11?e=%2BtSC5ziSlHPn2KeJMhhJbsldWfV6oeO4H5ymIt%2FCkol6sjcJDrF11IBQVx%2B2TRc%3D&p=mm_32233099_3201984_10539553

  • 相关阅读:
    CentOS 7搭建SVN服务器
    CentOS 配置MySQL允许远程登录
    使用nginx实现基于tcp协议的https协议多域名指向的分别转发功能
    centos7 设置内核启动顺序
    nginx 针对特定地区的ip进行规则匹配
    【转】golang 交叉编译
    linux修改用户id,组id
    etcd 增减节点
    [转]etcd 启用 https
    windows 多网卡路由设置
  • 原文地址:https://www.cnblogs.com/xffy1028/p/2278341.html
Copyright © 2011-2022 走看看