jquery实现购物车基本功能

购物车里的功能无非是商品数量的加减、商品删除、全选反选等操作,其实现过程如下所示:

1.html代码:

<body>
 <div class="empty">
 购物车空空如也,<a href="javascript:void(0);" >快去选购吧</a>
 </div>
 <table border="2px solid #ccc" id="table">
 <thead>
 <th>
 <input type="checkbox" class="checkOnly" style="vertical-align:middle;margin-right:20px;">全选
 </th>
 <th>序号</th>
 <th>商品名称</th>
 <th>数量</th>
 <th>单价</th>
 <th>小计</th>
 <th>操作</th>
 </thead>
 <tbody>
 <tr>
 <td>
 <input type="checkbox" class="check">
 </td>
 <td class="num">1</td>
 <td>烤煎饼</td>
 <td>
 <span>
 <input type="button" value="-" class="reduces">
 <span class="span">1</span>
 <input type="button" value="+" class="adds">
 </span>
 </td>
 <td>单价:
 <span class="price">2</span>
 </td>
 <td>
 小计:
 <span class="prices">2</span>
 </td>
 <td>
 <a href="#" class="del">删除</a>
 </td>
 </tr>
 <tr>
 <td>
 <input type="checkbox" class="check">
 </td>
 <td class="num">2</td>
 <td>珍珠奶茶</td>
 <td>
 <span>
 <input type="button" value="-" class="reduces">
 <span class="span">1</span>
 <input type="button" value="+" class="adds">
 </span>
 </td>
 <td>单价:
 <span class="price">4</span>
 </td>
 <td>
 小计:
 <span class="prices">4</span>
 </td>
 <td>
 <a href="#" class="del">删除</a>
 </td>
 </tr>
 <tr>
 <td>
 <input type="checkbox" class="check">
 </td>
 <td class="num">3</td>
 <td>水煮鱼</td>
 <td>
 <span>
 <input type="button" value="-" class="reduces">
 <span class="span">1</span>
 <input type="button" value="+" class="adds">
 </span>
 </td>
 <td>单价:
 <span class="price">20</span>
 </td>
 <td>
 小计:
 <span class="prices">20</span>
 </td>
 <td>
 <a href="#" class="del">删除</a>
 </td>
 </tr>
 <tr>
 <td>
 <input type="checkbox" class="check">
 </td>
 <td class="num">4</td>
 <td>蛋糕</td>
 <td>
 <span>
 <input type="button" value="-" class="reduces">
 <span class="span">1</span>
 <input type="button" value="+" class="adds">
 </span>
 </td>
 <td>单价:
 <span class="price">50</span>
 </td>
 <td>
 小计:
 <span class="prices">50</span>
 </td>
 <td>
 <a href="#" class="del">删除</a>
 </td>
 </tr>
 <tr>
 <td>
 <input type="checkbox" class="check">
 </td>
 <td class="num">5</td>
 <td>土豆片</td>
 <td>
 <span>
 <input type="button" value="-" class="reduces">
 <span class="span">1</span>
 <input type="button" value="+" class="adds">
 </span>
 </td>
 <td>单价:
 <span class="price">5</span>
 </td>
 <td>
 小计:
 <span class="prices">5</span>
 </td>
 <td>
 <a href="#" class="del">删除</a>
 </td>
 </tr>
 <tr>
 <td>
 <input type="checkbox" class="check">
 </td>
 <td class="num">6</td>
 <td>蛋黄派</td>
 <td>
 <span>
 <input type="button" value="-" class="reduces">
 <span class="span">1</span>
 <input type="button" value="+" class="adds">
 </span>
 </td>
 <td>单价:
 <span class="price">5.5</span>
 </td>
 <td>
 小计:
 <span class="prices">5.5</span>
 </td>
 <td>
 <a href="#" class="del">删除</a>
 </td>
 </tr>
 <tr>
 <td colspan="7" class="talast">
 <span>商品一共
 <span class="goods_num" style="color:red;font-size:20px;">0</span> 件; 共计花费
 <span class="pricetal" style="color:red;font-size:20px;">0</span> 元; 其中最贵的商品单价是
 <span class="pricest" style="color:red;font-size:20px;">0</span> 元</span>
 </td>
 </tr>
 </tbody>
 </table>
</body>

2.css代码:

<style type="text/css">
 table {
 width: 1000px;
 /* height: 300px; */
 border-collapse: collapse;
 table-layout: fixed;
 text-align: center;
 font-size: 18px;
 margin: 0 auto;
 }

 a {
 text-decoration: none;
 color: black;
 }

 tr {
 height: 50px;
 }

 .check {
 width: 20px;
 height: 20px;
 }

 .checkOnly {
 width: 20px;
 height: 20px;
 }

 .empty {
 font-size: 25px;
 position: fixed;
 top: 45%;
 left: 45%;
 display: none;
 }

 .empty a {
 color: pink;
 }

 .empty a:hover {
 text-decoration: underline;
 }
 </style>

3.js代码:

<script src="js/jquery-1.8.3.min.js"></script> //引入jquery
<script src="js/Allcheck.js"></script> //引入封装好的全选反选插件
<script>
 $(function () {
 $(".adds").each(function () { //点击增加的按钮
 $(this).click(function () {
 //1.改变数量
 var count = parseFloat($(this).parents("tr").find(".span").html());
 count++;
 $(this).parent("span").find(".span").html(count);
 //2.改小计(先找到单价与数量,再相乘,最后放在小计(“.prices”)里)
 var price = parseFloat($(this).parents("tr").find(".price").html());
 var money = (price * count);
 $(this).parents("tr").find(".prices").html(money);
 //3.改总价
 total();
 countAll();//最后的总数量
 compare();//选中复选框时比较商品单价中最高的
 });
 });
 $(".reduces").each(function () {//点击减少的按钮
 $(this).click(function () {
 //1.改变数量
 var count = parseFloat($(this).parents("tr").find(".span").html());
 count--;
 if (count < 1) { //当数量减到1时不能再减
 return;
 }
 $(this).parent("span").find(".span").html(count);
 //2.改小计
 var price = parseFloat($(this).parents("tr").find(".price").html());
 var money = (price * count);
 $(this).parents("tr").find(".prices").html(money);
 total();
 countAll();//最后的总数量
 compare();//选中复选框时比较商品单价中最高的
 });
 });
 //删除
 $(".del").each(function () {
 $(this).click(function () {
 let re = $(this).parents("tr"); //找到要删除的行
 if (confirm("你确定删除吗?")) {
 re.remove();
 }
 total();
 countAll(); //总数量
 compare(); //最贵的
 //删除后重新排序号
 for (let i = 0; i < $(".num").length; i++) {
 $(".num")[i].innerHTML = i + 1;
 }
 //全都删除时清空table(通过获取tbody的高度来判断)
 let clear = $("tbody").height();
 if (clear == 50) {
 $("table").remove();
 $(".empty").css({ //删完时显示"购物车为空"这个盒子
 display: "block"
 });
 }
 });
 });
 //合计
 function total() {
 let sum = 0;
 $(".prices").each(function () {//先循环每个小计
 if (($(this).parents("tr").find(".check")).prop("checked")) {//判断复选框有没有选中
 sum += parseFloat($(this).html());
 }
 $(".pricetal").html(sum);
 });
 }
 //总数量
 function countAll() {
 let counts = 0;
 for(let i=0;i<$(".check").length;i++){
 if($(".check")[i].checked==true){ //注意此块用jquery不好实现
 counts+=parseInt( $('.span')[i].innerHTML);
 }
 }
 $(".goods_num")[0].innerHTML=counts;
 }
 //最贵商品比较
 function compare() {
 let temp = 0;
 for (let i = 0; i < $(".price").length; i++) { //循环单价
 if($(".check")[i].checked==true){
 var temps = parseFloat($(".price")[i].innerHTML);
 if (temps > temp) {
 temp = temps;
 }
 }
 };
 $(".pricest").html(temp);
 }
 //全选插件(引入插件Allcheck.js)
 $(".checkOnly").bindCheck($("#table :checkbox"));
 //当点击复选框时调用total()
 $(".check").each(function (){
 $(this).click(function () {
 let num = 0;
 $(".check").each(function () {
 if ($(this).prop("checked")) {
 num++;
 }
 countAll();
 total();
 compare(); //最贵的
 });
 if(num == $(".check").length){//如果复选框的长度与num相等时,全选那个按钮也会被选中
 $(".checkOnly")[0].checked = true;
 compare(); //最贵的
 countAll(); //总数量
 total();
 }else{
 $(".checkOnly")[0].checked = false;
 }
 });
 });
 $(".checkOnly").click(function () {
 total();
 countAll(); //总数量
 compare(); //最贵的
 });
 });
</script>

补充上面js代码中用到的全选反选插件 \color{red}{补充上面js代码中用到的全选反选插件}补充上面js代码中用到的全选反选插件

//1、定义全选的插件
jQuery.fn.extend({
 bindCheck:function($subCheckBox,$btnUncheck){
 let $allCheckBox = this;
 //1、给全选复选框绑定click事件
 //this:是全选复选框(jQuery对象)
 this.click(function(){
 let isChecked = this.checked;
 $subCheckBox.each(function(){
 this.checked = isChecked;
 });
 });
 //2、给反选
 if(arguments.length==2){
 $btnUncheck.click(function(){
 $subCheckBox.each(function(){
 this.checked = !this.checked;
 });
 reversCheck();
 });
 }
 //3、给每个选择项的复选框绑定事件
 $subCheckBox.click(function(){
 reversCheck();
 });
 function reversCheck(){
 //1、判断是否全部的复选框被选中
 let isAllChecked = true;
 $subCheckBox.each(function(){
 if(!this.checked){
 isAllChecked = false;
 }
 });
 //2、处理全选复选框
 $allCheckBox.attr("checked",isAllChecked);
 }
 }
});

效果如下图所示:

以上就是一个功能比较完整的购物车,有问题或者建议欢迎指出。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 纯jquery实现模仿淘宝购物车结算

    这篇文章里,将会提到购物车里的所有功能.包括全选.单选金额改变.在增加数量时金额也会相应改变. 效果图展示: 说下大致的思路吧: 1.首先是计算一行的价格.这个功能在上篇博客里有提到,这里就不列举出来了. 2.遍历选中的几行,将每行的数值相加. 3.将值赋给总金额显示出来.当取消勾选或加减数量时,金额会相应改变. 下面是具体的js部分: <script type="text/javascript"> $(function(){ //计算总金额 function totalM

  • JQuery实现的购物车功能(可以减少或者添加商品并自动计算价格)

    购物车点击可以减少或者添加商品并自动计算价格: 购物车中可能有这样的功能,那就是点击按钮可以实现商品数量的减少或者增加,并且能够实时的计算出总的商品价格,下面就通过代码实例介绍一下如何实现此功能,当然下面的这个模拟实现的购物车难登大雅之堂,但是可以从中得到一些启发或者相关的知识点,代码如下: 复制代码 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title&

  • jQuery实现购物车计算价格功能的方法

    本文实例讲述了jQuery实现购物车计算价格功能的方法.分享给大家供大家参考.具体如下: 目的 实现在html界面修改购物车的件数,购物车商品价格的小计和总计要修改. 实现思路 1.当点击进入界面,刷新的时候触发body内的onload=""方法,跳转到JS代码.这样做的原因是在数据库内我们只会存储某客户的准备购买的商品件数,而不会存储每类商品价格的小计和购物车内所有物品的商品总价格,初始化的目的就是为将这些数字计算出来后显示在前台界面上. 2.当更改数量输入框中每个商品的数量时,整个

  • jquery购物车结算功能实现方法

    先看看购物车结算效果: 具体代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>购物车结算</title> <meta name=&qu

  • jQuery实现加入购物车飞入动画效果

    HTML 首先载入jQuery库文件和jquery.fly.min.js插件. 复制代码 代码如下: <script src="jquery.js"></script> <script src="jquery.fly.min.js"></script> 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片.价格.名称以及加入购物车按钮等信息. 复制代码 代码如下: <

  • 基于JQuery实现的类似购物商城的购物车

    商品信息使用JSON数据来模拟 同一个产品点击多次,不会重复添加,而是在已有的基础上数量+1, 商品数量也可以手动输入,当输入0时,该商品将自动从购物车删除(点击减号到小于1时,也会提示是否从购物车删除商品信息) 每个产品的价格和总价都会根据添加和删除的操作来动态计算 附下载链接:/201112/yuanma/jquery_gouwuche.rar 基本的功能都已经实现, 建议使用IE浏览器运行,其他浏览器没有测试 HTML代码: 复制代码 代码如下: <!DOCTYPE html PUBLIC

  • jQuery实现类似淘宝购物车全选状态示例

    复制代码 代码如下: <!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=&qu

  • jQuery实现购物车多物品数量的加减+总价计算

    复制代码 代码如下: <!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> <title>jQuery实现购物

  • jQuery+HTML5加入购物车代码分享

    这是一款基于jquery+html5实现的支持累加计价的网站购物车代码,可以把货物添加到购物车,添加物品数量,如果想取消购置某物品,这个功能也是可以实现的. 运行效果图:-----------------------------------查看效果----------------------------------- 为大家分享的jQuery+HTML5加入购物车代码如下 <head lang="en"> <meta charset="UTF-8"

  • jQuery实现购物车数字加减效果

    我们在网上购物提交订单时,在网页上一般会有一个选择数量的控件,要求买家选择购买商品的件数,开发者会把该控件做成可以通过点击实现加减等微调操作,当然也可以直接输入数字件数.本文将介绍常见的几种使用spinner数字微调器来实现数字加减的功能的方法. 左右加减数字 像京东提交订单时目前使用的是左右加减数字的效果,这个效果直接明了,操作简单.我们使用jquery.spinner.js插件实现左右加减数字,调用方法非常简单,请看演示示例1. 复制代码 代码如下: <input type="text

随机推荐