前端js-----购物车全功能(jQuery)

news/2024/7/11 1:14:53 标签: javascript, jquery, 前端

效果图:
在这里插入图片描述
HTML&&CSS:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../jquery-3.4.1.min.js">javascript"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .tab {
            width: 500px;
            border-collapse: collapse;
            margin: 0 auto;
        }
        
        .tab td,
        th {
            border: 1px solid #000;
        }
        
        .tab .num {
            width: 20px;
        }
        
        .tab .add,
        .sub {
            width: 20px;
        }
        
        .current {
            background-color: pink;
        }
    </style>
</head>

<body>
    <table class="tab">
        <thead>
            <th>全选 <input type="checkbox" name="" value="" class="checkAll">
                <input type="checkbox" name="" value="" class="checkAll">
            </th>
            <th>商品名称</th>
            <th>单价</th>
            <th>数量</th>
            <th>小计</th>
            <th>操作</th>
        </thead>
        <tbody>
            <tr>
                <td><input type="checkbox" class="ed" /></td>
                <td>电脑</td>
                <td class="price">¥200.20</td>
                <td>
                    <button type="button" class="sub">-</button>
                    <input type="text" name="" value="1" class="num">
                    <button type="button" class="add">+</button>
                </td>
                <td class="small_total">¥200.20</td>
                <td class="delete">删除</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="ed" /></td>
                <td>手机</td>
                <td class="price">¥100.30</td>
                <td>
                    <button type="button" class="sub">-</button>
                    <input type="text" name="" value="1" class="num">
                    <button type="button" class="add">+</button>
                </td>
                <td class="small_total">¥100.30</td>
                <td class="delete">删除</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="ed" /></td>
                <td>空调</td>
                <td class="price">¥1000.99</td>
                <td>
                    <button type="button" class="sub">-</button>
                    <input type="text" name="" value="1" class="num">
                    <button type="button" class="add">+</button>
                </td>
                <td class="small_total">¥1000.99</td>
                <td class="delete">删除</td>
            </tr>
        </tbody>
    </table>
    <div>
        <span>已选<span style="color: red;" class="num_sum">1</span>件商品</span>
        <span>总计:</span>
        <span class="sum" style="color: red;">0</span>
        <div><span style="color: red;" class="delSome">删除选中商品</span>
            <span style="color: red;" class="delAll">清空购物车</span>
        </div>
    </div>
    </body>

</html>

JS:

javascript">        //里面三个小的复选按钮选中状态跟着 全选按钮走
        //因为checked是复选框的固有属性,此时利用prop()获取和设置该属性
        $(function() {
            getSum();
            $(".checkAll").change(function() {
                    // console.log($(this).prop("checked"));//全选按钮的状态
                    $(".ed,.checkAll").prop("checked", $(this).prop("checked"));
                    getSum();
                    if ($(".ed,.checkAll").prop("checked")) {
                        //如果全选,让所有商品添加类名(背景颜色)
                        $(".tab tbody").children().addClass("current");
                    } else {
                        $(".tab tbody").children().removeClass("current");
                    }
                })
                //如果所有小按钮的个数都被选了,全选按钮就选上,如果小按钮没有被选上,则全选按钮就不选上
                //:checked选择器,查找本选中的表单元素
            $(".ed").change(function() {
                // console.log($(".ed:checked").length);//小复选框选中的个数
                // console.log($(".ed").length);
                //console.log($(this).prop("checked"));
                if ($(".ed:checked").length === $(".ed").length) {
                    $(".checkAll").prop("checked", true);
                } else {
                    $(".checkAll").prop("checked", false);
                }
                getSum();
                if ($(this).prop("checked")) {
                    $(this).parents("tr").addClass("current");
                } else {
                    $(this).parents("tr").removeClass("current");
                }
            })

            $(".add").click(function() {
                let n = parseInt($(this).siblings(".num").val());
                //console.log(n);
                n++;
                $(this).siblings(".num").val(n);
                let price = $(this).parent().siblings(".price").html();
                price = price.substr(1);
                //console.log(price);
                $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
                getSum();
            })
            $(".sub").click(function() {
                    let n = parseInt($(this).siblings(".num").val());
                    //console.log(n);
                    if (n === 1) {
                        return false;
                    }
                    n--;
                    $(this).siblings(".num").val(n);
                    let price = $(this).parent().siblings(".price").html();
                    price = price.substr(1);
                    //console.log(price);
                    $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
                    getSum();
                })
                //用户也可以直接修改表单num里面的值(小bug),同样计算小计
            $(".num").change(function() {
                let n = $(this).val();
                let price = $(this).parent().siblings(".price").html();
                price = price.substr(1);
                $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
                getSum();
            })

            function getSum() {
                let count = 0; //计算总件数
                let money = 0; //计算总价钱
                $(".num").each(function(index) {
                    if ($(".ed").eq(index).prop("checked") == true) {
                        count += parseInt($(".num").eq(index).val());
                        money += parseFloat($(".small_total").eq(index).text().substr(1));
                    }
                })
                $(".num_sum").html(count);
                $(".sum").html(money.toFixed(2));
            }

            //删除商品模块
            //点击删除之后一定是删除当前的商品,所以从$(this)出发
            $(".delete").click(function() {
                    //删除的是当前的商品
                    $(this).parent().remove();
                    $(".ed").change();
                    getSum();
                    clearCheckAll();
                })
                //删除选定的商品:小的复选框如果选中就删除对应的商品
            $(".delSome").click(function() {
                    //删除的是选中的商品
                    $(".ed:checked").parent().parent().remove();
                    getSum();
                    clearCheckAll();
                })
                //清空购物车
            $(".delAll").click(function() {
                $(".tab tbody").empty();
                getSum();
                clearCheckAll();
            })

            function clearCheckAll() {
                if ($(".tab tbody")[0].innerText == '') {
                    $(".checkAll").prop("checked", false);
                }
            }
        })

http://www.niftyadmin.cn/n/1143457.html

相关文章

快速幂与64位整数乘法(位运算)

矩阵快速幂和64位整数乘法 矩阵快速幂64位整数乘法 矩阵快速幂 问题链接&#xff1a; AcWing 90. 64位整数乘法 问题描述&#xff1a; 分析 快速幂需要用到位运算的思想&#xff0c;求 a b a^b ab&#xff0c;我们将 b b b转换成二进制的形式&#xff0c;假设 b 7 b7 b7&…

前端js-----选项卡/tab栏(面向对象)

功能效果如下&#xff1a; 实现代码&#xff1a; HTML&#xff1a; <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"…

前端js-----抽奖功能(异步)

效果如下&#xff1a; 代码如下&#xff1a; HTML&#xff1a; <body><div id"father"><div class"option">抱抱</div><div class"option">亲亲</div><div class"option">举高高</div…

JS中的数组,对象,字符串常用的方法

JS中的数组&#xff0c;对象&#xff0c;字符串常用的方法 数组的常用方法&#xff1a; var arr [1,2,3,4,5]; arr[0] arr[arr.length-1]//通过下标找到数组中指定的元素&#xff0c;访问数组的元素 arr.join("-") //数组转成字符串 arr.toString() //数组转成字符串…

前端js-----模糊搜索(indexOf)

效果&#xff1a; 原生js实现代码&#xff1a; <!DOCTYPE html> <html><head><meta charset"utf-8"><title></title><style type"text/css">* {margin: 0;padding: 0;}.box {overflow: hidden;width: 400px;he…

前端js-----三级联动

效果如下&#xff08;原生js&#xff09;&#xff1a; ------ HTML&#xff1a; <body>请选择省份<select name"" id"province"><option value"请选择省份">请选择省份</option></select>请选择市<select na…

前端js-----轮播图(焦点图)实现原理

轮播&#xff0c;无论是文字轮播还是图片轮播&#xff0c;他们的原理都是一样的&#xff0c;都是通过定时器setInterval执行循环展示和隐藏。 原理&#xff1a; 一系列的图片平铺&#xff0c;一般是放在li里面&#xff0c;只显示一张图片&#xff0c;其余图片隐藏over-follow:h…

前端js-----vue中的key

在官方文档中是这样解释的&#xff1a;key的特殊属性主要用在vue的虚拟DOM算法&#xff0c;在新旧nodes对比时辨识VNodes。如果不能使用key&#xff0c;Vue会使用一种最大 限度减少动态元素并且尽可能的尝试就地修改或修复相同类型元素的算法。而且使用key时&#xff0c;它会基…