广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >jQuery实现全部购物车功能实例
  • 598
分享到

jQuery实现全部购物车功能实例

2024-04-02 19:04:59 598人浏览 独家记忆
摘要

目录一、全选二、增减商品数量三、修改商品小计四、计算总计和总和五、删除商品 六、选中商品添加背景七、html 全部核心代码今天是一些购物车的基本功能实现,全选、增减商品数量、修改商品

今天是一些购物车的基本功能实现,全选增减商品数量修改商品小计计算总计和总和删除商品选中添加背景颜色等一些常见功能。

 html结构的全部代码都在文末了,懂的都懂啊!!!

一、全选

全选分析:

  • 全选思路:里面3个小的复选框按钮(j-checkbox)选中状态(checked)跟着全选按钮(checkall)走。
  • 因为checked 是复选框的固有属性,此时我们需要利用prop()方法获取和设置该属性。
  • 把全选按钮状态赋值给3小复选框就可以了。
  • 当我们每次点击小的复选框按钮,就来判断:
  • 如果小复选框被选中的个数等于3 就应该把全选按钮选上,否则全选按钮不选。
  • :checked 选择器 :checked 查找被选中的表单元素。

 以上是我们要实现的一个基本功能展示,接下来进入案例中来写一写。


// 全选
    $(".checkall").change(function () {
        $(".j-checkbox,.checkall").prop("checked", $(this).prop("checked"))
    });
 
    $(".j-checkbox").change(function () {
        if ($(".j-checkbox:checked").length === $(".j-checkbox").length) {
            $(".checkall").prop("checked", true);
        } else {
            $(".checkall").prop("checked", false);
        }
    });

二、增减商品数量

增减商品数量分析:

  • 核心思路:首先声明一个变量,当我们点击+号(increment),就让这个值++,然后赋值给文本框。
  • 注意1: 只能增加本商品的数量, 就是当前+号的兄弟文本框(itxt)的值。
  • 修改表单的值是val() 方法
  • 注意2: 这个变量初始值应该是这个文本框的值,在这个值的基础上++。要获取表单的值
  • 减号(decrement)思路同理,但是如果文本框的值是1,就不能再减了。

点击加号数量增加,点击减少数量减少这是一个基本的加减功能的实现,最少大于等于一件商品

给加号和减号分别一个点击事件,然后再获取表单里面的值,用一个变量来自增自减来改变里面的值。


    // 数量 加
    $(".increment").click(function () {
        var n = $(this).siblings(".itxt").val();
        n++;
        $(this).siblings(".itxt").val(n);
    })
    // 数量 减
    $(".decrement").click(function () {
        var n = $(this).siblings(".itxt").val();
        if (n == 1) {
            return false;
        }
        n--;
        $(this).siblings(".itxt").val(n);
    });

三、修改商品小计

修改商品小计分析:

  • 核心思路:每次点击+号或者-号,根据文本框的值 乘以 当前商品的价格 就是 商品的小计
  • 注意1: 只能增加本商品的小计, 就是当前商品的小计模块(p-sum)
  • 修改普通元素的内容是text() 方法
  • 注意2: 当前商品的价格,要把¥符号去掉再相乘 截取字符串 substr(1)
  • parents(‘选择器') 可以返回指定祖先元素
  • 最后计算的结果如果想要保留2位小数 通过 toFixed(2) 方法
  • 用户也可以直接修改表单里面的值,同样要计算小计。 用表单change事件
  • 用最新的表单内的值 乘以 单价即可 但是还是当前商品小计

因为是点击加减号才修改小计里面的值,所以这里的代码要写在加减号的点击事件里面,当点击这个事件以后,取出单价里面的值乘以数量里的值就可以得到总的价钱,这里就是整体代码的基本实现过程稿了。


    // 数量 加
    $(".increment").click(function () {
        var n = $(this).siblings(".itxt").val();
        n++;
        $(this).siblings(".itxt").val(n);
 
        // 修改商品小计 
        var p = $(this).parents(".p-num").siblings(".p-price").html();
        p = p.substr(1);
        var price = (p * n).toFixed(2);
        $(this).parent().parent().siblings(".p-sum").html("¥" + price);
        getSum();
    })
    // 数量 减
    $(".decrement").click(function () {
        var n = $(this).siblings(".itxt").val();
        if (n == 1) {
            return false;
        }
        n--;
        $(this).siblings(".itxt").val(n);
        // 修改商品小计 
        var p = $(this).parents(".p-num").siblings(".p-price").html();
        p = p.substr(1);
        var price = (p * n).toFixed(2);
        $(this).parent().parent().siblings(".p-sum").html("¥" + price);
        getSum();
    });

四、计算总计和总和

计算总计和总和分析:

  • 核心思路:把所有文本框里面的值相加就是总计数量。总额同理
  • 文本框里面的值不相同,如果想要相加需要用到each遍历。声明一个变量,相加即可
  • 点击+号-号,会改变总计和总额,如果用户修改了文本框里面的值同样会改变总计和总额
  • 因此可以封装一个函数求总计和总额的, 以上2个操作调用这个函数即可。
  • 注意1: 总计是文本框里面的值相加用 val() 总额是普通元素的内容用text()
  • 要注意普通元素里面的内容要去掉¥并且转换为数字型才能相加

本质应该是选中复选框,该商品才会被统计到结算栏里,这里我写的是购物车里面所以数量和小计的总和,所以数量以改变小计也在变,相应的也在影响着结算栏里面的数据。


    // 封装结算商品的个数及价钱
    getSum();
    function getSum() {
        var count = 0;
        var money = 0;
        $(".itxt").each(function (i, ele) {
            count += parseInt($(ele).val());
        });
        $(".amount-sum em").text(count);
 
        $(".p-sum").each(function (i, ele) {
            money += parseFloat($(ele).text().substr(1))
        });
        $(".price-sum em").text("¥" + money.toFixed(2));
    }

五、删除商品 

删除商品模块分析:

  • 核心思路:把商品remove() 删除元素即可
  • 有三个地方需要删除: 1. 商品后面的删除按钮 2. 删除选中的商品 3. 清理购物车
  • 商品后面的删除按钮: 一定是删除当前的商品,所以从 $(this) 出发
  • 删除选中的商品: 先判断小的复选框按钮是否选中状态,如果是选中,则删除对应的商品
  • 清理购物车: 则是把所有的商品全部删掉

单击商品后面删除就删除此商品,点击下面的删除选中的商品就删除复选框中选中的商品,单机清理购物车就删除购物车里面所有商品。


    // 删除
    // 商品后面的删除
    $(".p-action a").click(function () {
        $(this).parents(".cart-item").remove();
        getSum();
    });
    // 删除选中的商品
    $(".remove-batch").click(function () {
        $(".j-checkbox:checked").parents(".cart-item").remove();
        getSum();
    })
    // 清空购物车
    $(".clear-all").click(function () {
        $(".cart-item").remove();
        getSum();
    })

六、选中商品添加背景

选中商品添加背景分析:

  • 核心思路:选中的商品添加背景,不选中移除背景即可
  • 全选按钮点击:如果全选是选中的,则所有的商品添加背景,否则移除背景
  • 小的复选框点击: 如果是选中状态,则当前商品添加背景,否则移除背景
  • 这个背景,可以通过类名修改,添加类和删除类

是选中就添加背景颜色,所以这里的代码应该写在复选框改变的事件里面。


    // 全选
    $(".checkall").change(function () {
        $(".j-checkbox,.checkall").prop("checked", $(this).prop("checked"))
        // 给商品添加背景颜色
        if ($(this).prop("checked")) {
            $(".cart-item").addClass("check-cart-item");
        } else {
            $(".cart-item").removeClass("check-cart-item");
        }
    });
 
    $(".j-checkbox").change(function () {
        if ($(".j-checkbox:checked").length === $(".j-checkbox").length) {
            $(".checkall").prop("checked", true);
        } else {
            $(".checkall").prop("checked", false);
        }
        // 给商品添加背景颜色
        if ($(this).prop("checked")) {
            $(this).parents(".cart-item").addClass("check-cart-item");
        } else {
            $(this).parents(".cart-item").removeClass("check-cart-item");
        }
    });

七、html 全部核心代码


<div class="c-container">
        <div class="w">
            <div class="cart-filter-bar">
                <em>全部商品</em>
            </div>
            <!-- 购物车主要核心区域 -->
            <div class="cart-warp">
                <!-- 头部全选模块 -->
                <div class="cart-thead">
                    <div class="t-checkbox">
                        <input type="checkbox" name="" id="" class="checkall"> 全选
                    </div>
                    <div class="t-Goods">商品</div>
                    <div class="t-price">单价</div>
                    <div class="t-num">数量</div>
                    <div class="t-sum">小计</div>
                    <div class="t-action">操作</div>
                </div>
                <!-- 商品详细模块 -->
                <div class="cart-item-list">
                    <div class="cart-item check-cart-item">
                        <div class="p-checkbox">
                            <input type="checkbox" name="" id="" checked class="j-checkbox">
                        </div>
                        <div class="p-goods">
                            <div class="p-img">
                                <img src="upload/p1.jpg" alt="">
                            </div>
                            <div class="p-msg">【5本26.8元】经典儿童文学彩图青少版八十天环游地球中学生语文教学大纲</div>
                        </div>
                        <div class="p-price">¥12.60</div>
                        <div class="p-num">
                            <div class="quantity-fORM">
                                <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="decrement">-</a>
                                <input type="text" class="itxt" value="1">
                                <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="increment">+</a>
                            </div>
                        </div>
                        <div class="p-sum">¥12.60</div>
                        <div class="p-action"><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >删除</a></div>
                    </div>
                    <div class="cart-item">
                        <div class="p-checkbox">
                            <input type="checkbox" name="" id="" class="j-checkbox">
                        </div>
                        <div class="p-goods">
                            <div class="p-img">
                                <img src="upload/p2.jpg" alt="">
                            </div>
                            <div class="p-msg">【2000张贴纸】贴纸书 3-6岁 贴画儿童 贴画书全套12册 贴画 贴纸儿童 汽</div>
                        </div>
                        <div class="p-price">¥24.80</div>
                        <div class="p-num">
                            <div class="quantity-form">
                                <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="decrement">-</a>
                                <input type="text" class="itxt" value="1">
                                <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="increment">+</a>
                            </div>
                        </div>
                        <div class="p-sum">¥24.80</div>
                        <div class="p-action"><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >删除</a></div>
                    </div>
                    <div class="cart-item">
                        <div class="p-checkbox">
                            <input type="checkbox" name="" id="" class="j-checkbox">
                        </div>
                        <div class="p-goods">
                            <div class="p-img">
                                <img src="upload/p3.jpg" alt="">
                            </div>
                            <div class="p-msg">唐诗三百首+成语故事全2册 一年级课外书 精装注音儿童版 小学生二三年级课外阅读书籍</div>
                        </div>
                        <div class="p-price">¥29.80</div>
                        <div class="p-num">
                            <div class="quantity-form">
                                <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="decrement">-</a>
                                <input type="text" class="itxt" value="1">
                                <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="increment">+</a>
                            </div>
                        </div>
                        <div class="p-sum">¥29.80</div>
                        <div class="p-action"><a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >删除</a></div>
                    </div>
                </div>
 
                <!-- 结算模块 -->
                <div class="cart-floatbar">
                    <div class="select-all">
                        <input type="checkbox" name="" id="" class="checkall">全选
                    </div>
                    <div class="operation">
                        <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="remove-batch"> 删除选中的商品</a>
                        <a href="javascript:;" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  class="clear-all">清理购物车</a>
                    </div>
                    <div class="toolbar-right">
                        <div class="amount-sum">已经选<em>1</em>件商品</div>
                        <div class="price-sum">总价: <em>¥12.60</em></div>
                        <div class="btn-area">去结算</div>
                    </div>
                </div>
            </div>
        </div>
 
    </div>

以上所述是小编给大家介绍的Jquery实现全部购物车功能实例,希望对大家有所帮助。在此也非常感谢大家对编程网网站的支持!

--结束END--

本文标题: jQuery实现全部购物车功能实例

本文链接: https://www.lsjlt.com/news/158711.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • jQuery实现全部购物车功能实例
    目录一、全选二、增减商品数量三、修改商品小计四、计算总计和总和五、删除商品 六、选中商品添加背景七、html 全部核心代码今天是一些购物车的基本功能实现,全选、增减商品数量、修改商品...
    99+
    2022-11-12
  • jquery实现购物车功能
    本文实例为大家分享了jquery实现购物车功能的具体代码,供大家参考,具体内容如下 html ​<!DOCTYPE html> <html> ...
    99+
    2022-11-12
  • jquery怎么实现购物车功能
    本篇内容主要讲解“jquery怎么实现购物车功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“jquery怎么实现购物车功能”吧!html<!DOCTYPE html>&l...
    99+
    2023-06-20
  • jQuery实现购物车
    本文实例为大家分享了jQuery实现购物车的具体代码,供大家参考,具体内容如下 1.描述 2.HTML布局 <div>         <button>全选...
    99+
    2022-11-13
  • vue实现购物车全部功能的简单方法
    主要功能如下: 增加商品信息 修改商品信息 删除单个商品 删除多个商品 清空购物车 对商品单价进行降序排序 根据商品名称实现...
    99+
    2022-11-12
  • javaweb实现购物车功能
    本篇文章讲的是如何使用javaweb相关知识模拟购物车功能 (web练手小项目) 使用到的相关知识(部分知识点在文章中简单涉及到):        html  cs  javascript  jsp  servlet   ajax  jQu...
    99+
    2023-10-24
    mybatis java mysql servlet tomcat
  • python实现购物车功能
    本文实例为大家分享了python实现购物车功能的具体代码,供大家参考,具体内容如下 功能要求: 要求用户输入总资产,例如:2000显示商品列表,让用户根据序号选择商品,加入购物车购买...
    99+
    2022-11-13
  • python购物车功能实现
    name = "gaowang" pwd = "123.abc" list_he=[]          #定义空列表,后面接收 for i in range(3):     username = input("请输入您的账号:")    ...
    99+
    2023-01-31
    购物车 功能 python
  • 如何使用jQuery实现购物车结算功能
    这篇文章主要介绍如何使用jQuery实现购物车结算功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!具体内容如下<!DOCTYPE html> <html...
    99+
    2022-10-19
  • jQuery怎么模拟实现淘宝购物车功能
    这篇文章主要讲解了“jQuery怎么模拟实现淘宝购物车功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“jQuery怎么模拟实现淘宝购物车功能”吧!首先我们要实现的内容的需求有如下几点:1....
    99+
    2023-06-04
  • Android实现简单购物车功能
    本文实例为大家分享了Android实现购物车功能的具体代码,供大家参考,具体内容如下MainActivity布局:<?xml version="1.0" encoding="utf-8"?><LinearL...
    99+
    2023-05-30
    android 购物车 roi
  • vue实现书籍购物车功能
    本文实例为大家分享了vue实现书籍购物车功能的具体代码,供大家参考,具体内容如下 效果图 点击增加、减少购买数量和移除总价格会变化 代码 <!DOCTYPE html&g...
    99+
    2022-11-12
  • vue实现购物车完整功能
    vue实现购物车商品单选、全选及商品数量和总价计算,供大家参考,具体内容如下 效果展示 HTML <template>   <div class="buyCar"...
    99+
    2022-11-13
  • Vue2.0怎么实现购物车功能
    这篇文章将为大家详细讲解有关Vue2.0怎么实现购物车功能,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。简介vue.js是由华人尤雨溪开发的一套MVVM框架。vue.js...
    99+
    2022-10-19
  • 如何实现IONIC购物车功能
    这篇文章主要为大家展示了“如何实现IONIC购物车功能”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何实现IONIC购物车功能”这篇文章吧。具体内容如下HTM...
    99+
    2022-10-19
  • vue如果实现购物车功能
    本文小编为大家详细介绍“vue如果实现购物车功能”,内容详细,步骤清晰,细节处理妥当,希望这篇“vue如果实现购物车功能”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。 原理分析...
    99+
    2022-10-19
  • js实现简易购物车功能
    本文实例为大家分享了js实现简易购物车功能的具体代码,供大家参考,具体内容如下 一.整体效果图 (关灯下)  (开灯下) 二.HTML代码 <!DOCTYPE...
    99+
    2022-11-12
  • Vue.js框架实现购物车功能
    本文实例为大家分享了Vue.js框架实现购物车的具体代码,供大家参考,具体内容如下 <!DOCTYPE html> <html lang="en" xmlns:...
    99+
    2022-11-12
  • vue如何实现购物车功能
    本篇内容主要讲解“vue如何实现购物车功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“vue如何实现购物车功能”吧!如图,需要有加入购物车的标识思路如下:点击购物车按钮时将商品的id,titl...
    99+
    2023-06-30
  • php购物车功能怎么实现
    要实现PHP购物车功能,您需要遵循以下步骤:1. 创建一个数据库表来存储购物车数据,例如:cart_items。表中应包含以下字段:...
    99+
    2023-05-31
    php购物车 php
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作