<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<script src="jquery-3.2.1.min.js"></script>
</head>
<body>
<div id="main">
<table class="table table-striped">
<thead>
<tr>
<th scope="col">标号</th>
<th scope="col">名称</th>
<th scope="col">价格</th>
<th scope="col">数量</th>
<th scope="col">小计</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">
<input type="checkbox" name="ck" class="ck">
</th>
<td>商品一号</td>
<td>9.91</td>
<td>
<button class="btn-up">+</button>
<input type="text" name="num" style="width: 30px" value="1">
<button class="btn-down">-</button>
</td>
<td class="subtotal">9.91</td>
<td><button class="btn-danger">删除</button></td>
</tr>
<tr>
<th scope="row">
<input type="checkbox" name="ck" class="ck">
</th>
<td>商品二号</td>
<td>9.9</td>
<td>
<button class="btn-up">+</button>
<input type="text" name="num" style="width: 30px" value="1">
<button class="btn-down">-</button>
</td>
<td class="subtotal">9.9</td>
<td><button class="btn-danger">删除</button></td>
</tr>
<tr>
<th scope="row">
<input type="checkbox" name="ck" class="ck">
</th>
<td>商品三号</td>
<td>100.23</td>
<td>
<button class="btn-up">+</button>
<input type="text" name="num" style="width: 30px" value="1">
<button class="btn-down">-</button>
</td>
<td class="subtotal">100.23</td>
<td><button class="btn-danger">删除</button></td>
</tr>
</tbody> </table>
<button id="qx">全选</button>
<button id="qbx">全不选</button>
<button id="fx">反选</button>
<button id="plsc">批量删除</button>
<p style="float: right">
总共购买<span id="counter">0</span> 件 ;支付总金额:<span id="total">0.00</span>¥
</p>
</div>
</body>
</html>
<script>
//加
$(".btn-up").click(function () {
var num = $(this).next().val();
num++;
var price = $(this).parent().prev().text();
var subTotal = price * num;
$(this).next().val(num);
$(this).parent().next().html(subTotal.toFixed(2));
$(this).parent().siblings(":first").find('.ck').prop("checked","checked");
computed();
});
//减
$(".btn-down").click(function () {
var num = $(this).prev().val();
if(num>1){
num--;
}
var price = $(this).parent().prev().text();
var subTotal = price * num;
$(this).prev().val(num);
$(this).parent().next().text(subTotal.toFixed(2)); computed();
});
//计算总金额
function computed(){
var total = 0;
var ele = $("input[name='ck']:checked");
var counter = ele.length;
ele.each(function (index,v) {
//
total += parseFloat( $(v).parent().parent().find('.subtotal').text());
});
$("#counter").html(counter);
$("#total").html(total).toFixed(2);
}
$(".ck").click(function () {
computed();
}); //全选
$("#qx").click(function () {
var ck = $("input[name='ck']");
for(var i=0;i<ck.length;i++){
ck[i].checked = true;
}
computed();
});
//全不选
$("#qbx").click(function () {
var ck = $("input[name='ck']");
for(var i=0;i<ck.length;i++){
ck[i].checked = false;
}
computed();
});
//反选
$("#fx").click(function () {
var ck = $("input[name='ck']");
for (var i=0;i<ck.length;i++){
if(ck[i].checked){
ck[i].checked = false;
}else{
ck[i].checked = true;
}
}
computed();
}); $(".btn-danger").click(function () {
$(this).parent().parent().empty();
}); $("#plsc").click(function () {
var ele = $("input[name='ck']:checked");
for(var i=0;i<ele.length;i++){
//$(ele[i]).parent().parent().remove();
$(ele[i]).parent().parent().remove();
}
computed();
}); </script>

  

JS-购物车的更多相关文章

  1. 简单实用angular.js购物车功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. js购物车计算价格

    <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta nam ...

  3. vue.js购物车

    vue.js https://cn.vuejs.org/v2/guide/ 简单购物车 <html> <head> <script src="https://c ...

  4. js 购物车的实现

    购物车原理:创建一个构造函数,把涉及到的项目写成方法,然后把这些方法放到构造函数的原型对象上,通过按钮绑定,调用原型对象上的方法,实现对涉及项目的改变 html代码: <!DOCTYPE htm ...

  5. 原生JS 购物车及购物页面的cookie使用

    ////////////////////////////////////购物页面 <!DOCTYPE html><html lang="en"><he ...

  6. JS购物车编辑

    实现了:第一件商品的加减实现了:全选/全不选(使用prop而不是attr)实现了:删除(遮罩层) 未实现:第二件商品的删除未实现:小计及应付款额的初始化(写死的) 计算小数乘法时,要先乘100 < ...

  7. js 购物车中,多件商品数量加减效果修改,实现总价随数量加减改变

    <!DOCTYPE html> <html> <head> <meta charset=UTF-8 /> <title>无标题文档</ ...

  8. js 购物车的数量加减,对应的总价也随机变化

    html相关的源码: <div class="goods_num clearfix"> <div class="num_name fl"> ...

  9. Vue node.js商城-购物车模块

      一.渲染购物车列表页面 新建src/views/Cart.vue获取cartList购物车列表数据就可以在页面中渲染出该用户的购物车列表数据 data(){   return {      car ...

  10. shoppingCart.js

    ylbtech-JavaScript-util: shoppingCart.js 购物车脚本 1.A,JS-效果图返回顶部   1.B,JS-Source Code(源代码)返回顶部 1.B.1,m. ...

随机推荐

  1. python中一个经典的参数错误

    直接上代码 class Company: def __init__(self, name, staffs=[]): self.name = name self.staffs = staffs def ...

  2. php7.3编译安装 支持微擎2.0

    再次整理   //一下配置在命令粘贴时注意句尾加 \ , 在 \ 后不能有空格,不然会自动执行,相当于回车./configure --prefix=/usr/local/php \ --with-co ...

  3. JVM收藏的文章

    JAVA 内存泄露详解(原因.例子及解决) https://blog.csdn.net/anxpp/article/details/51325838 JVM内存区域划分Eden Space.Survi ...

  4. python基础2-静态方法和类方法

    1. 类方法 是类对象所拥有的方法,需要用修饰器@classmethod来标识其为类方法,对于类方法,第一个参数必须是类对象,一般以cls作为第一个参数(当然可以用其他名称的变量作为其第一个参数,但是 ...

  5. 云端iclound使用-陈棚

    使用NSmetadataQuery查询文档,增加,删除,编辑 界面设计完成后效果如图: 程序清单:FKDiary.h @interface FKDiary : UIDocument @property ...

  6. k8s之list-watch机制、节点调度以及亲和性

    k8s之list-watch机制.节点调度以及亲和性 目录 k8s之list-watch机制.节点调度以及亲和性 一.list-watch机制 1. list-watch介绍 2. list-watc ...

  7. 文件I/O流、文件、FileInputStreaam、FileOutputStream、FileReader、FileWriter的介绍和使用

    一.文件:保存数据的地方 1.文件流:文件在程序中是以流的形式类操作的 类比: 流:数据在数据源(文件)和程序(内存)之间经历的路径 输入流:数据从数据源(文件)到程序(内存)的路径 输出流:数据从程 ...

  8. git本地仓库关联多个远程仓库及取消关联

    关联 本地和远程仓库关联步骤: 在网站上(如gitee.github等,这里以gitee为例)首先添加好自己的ssh公钥 本地创建一个空的项目,即文件夹. 在文件夹下git init初始化仓库 在gi ...

  9. HTML基础笔记整理

    「学习笔记」HTML基础 前言 勤做笔记不仅可以让自己学的扎实,更重要的是可以让自己少走弯路.有人说:"再次翻开笔记是什么感觉",我的回答是:"初恋般的感觉". ...

  10. PHP面试常考之会话控制

    你好,是我琉忆,欢迎您来到PHP面试专栏.本周(2019.2-25至3-1)的一三五更新的文章如下: 周一:PHP面试常考之会话控制周三:PHP面试常考之网络协议周五:PHP面试常考题之会话控制和网络 ...