JS-购物车
2024-10-19 22:19:57
<!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>
最新文章
- 【无私分享:ASP.NET CORE 项目实战(第九章)】创建区域Areas,添加TagHelper
- IntelliJ IDEA 远程调试
- zabbix安装全过程
- 携手K2 BPM,华住酒店完美实现“互联网+”转型
- EXEL表格读取 按键精灵
- 细雨学习笔记:Jmeter上一个请求的结果作为下一个请求的参数--使用正则提取器
- source Insight注册码
- Oracle11g新特性之动态变量窥视
- JS数组追加数组采用push.apply的坑(转)
- 树莓派变成一个Web服务器: nginx + php + sqlite
- 第一百一十八节,JavaScript,动态加载脚本和样式
- Error Code: 1044. Access denied for user &#39;root&#39;@&#39;%&#39; to database
- .net aop 操作 切面应用 Castle.Windsor框架 spring 可根据接口 自动生成一个空的实现接口的类
- canvas高级篇(转载)移动元素
- java 实验 三 (1)
- ATM Mechine (概率DP)
- 关于Redis缓存数据库
- SpringMVC——SpringMVC简介
- 20155320 EXP8 Web基础
- 前端学习 -- Css -- 行间距