网站首页 > 技术文章 正文
Html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<script src="js/jquery-3.4.1.min.js"></script>
<link rel="stylesheet" href="bootstrap-3.4.1/css/bootstrap.min.css">
<script src="js/jquery_shopping.js"></script>
</head>
<body>
<div class="panel-group">
<div class="panel panel-primary">
<table class="table table-bordered table-striped table-hover table-condensed" id="tab_s">
<thead>
<tr class="info">
<th>商品名称</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
<th>操作</th>
</tr>
</thead>
<tbody id="tby">
<tr>
<td>电脑</td>
<td class="price">¥3999.00</td>
<td>
<div class="row">
<div class="col-md-1">
<button type="button" class="btn btn-default btn-sm" name="sub">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button>
</div>
<div class="col-md-1">
<input class="form-control input-sm" type="text" name="num" value="1" placeholder="请输入数量">
</div>
<div class="col-md-1">
<button type="button" class="btn btn-default btn-sm" name="add">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
</div>
</div>
</td>
<td class="small_total">¥3999.00</td>
<td>
<button type="button" class="btn btn-default btn-sm" name="delete">
<span class="glyphicon glyphicon-remove"> 删除</span>
</button>
</td>
</tr>
<tr>
<td>手机</td>
<td class="price">¥1998.00</td>
<td>
<div class="row">
<div class="col-md-1">
<button type="button" class="btn btn-default btn-sm" name="sub">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button>
</div>
<div class="col-md-1">
<input class="form-control input-sm" type="text" name="num" value="1" placeholder="请输入数量">
</div>
<div class="col-md-1">
<button type="button" class="btn btn-default btn-sm" name="add">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
</div>
</div>
</td>
<td class="small_total">¥1998.00</td>
<td>
<button type="button" class="btn btn-default btn-sm" name="delete">
<span class="glyphicon glyphicon-remove"> 删除</span>
</button>
</td>
</tr>
<tr>
<td>空调</td>
<td class="price">¥2099.00</td>
<td>
<div class="row">
<div class="col-md-1">
<button type="button" class="btn btn-default btn-sm" name="sub">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</button>
</div>
<div class="col-md-1">
<input class="form-control input-sm" type="text" name="num" value="1" placeholder="请输入数量">
</div>
<div class="col-md-1">
<button type="button" class="btn btn-default btn-sm" name="add">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>
</div>
</div>
</td>
<td class="small_total">¥2099.00</td>
<td>
<button type="button" class="btn btn-default btn-sm" name="delete">
<span class="glyphicon glyphicon-remove"> 删除</span>
</button>
</td>
</tr>
</tbody>
</table>
<div class="panel-footer">
<span>已选<span style="color: red;" class="num_sum">1</span>件商品</span>
<span>总计:</span>
<span class="sum" style="color: red;">0</span>
<div>
<button type="button" class="btn btn-default btn-sm" name="delSome">
删除选中商品
</button>
<button type="button" class="btn btn-default btn-sm" name="delAll">
清空购物车
</button>
</div>
</div>
</div>
</div>
</body>
</html>
Js代码:
$(function(){
function initTableCheckbox() {
var $thr = $('table thead tr');
var $checkAllTh = $('<th><input type="checkbox" id="checkAll" name="checkAll" /></th>');
/*将全选/反选复选框添加到表头最前,即增加一列*/
$thr.prepend($checkAllTh);
var $checkAll = $thr.find('input');
$checkAll.click(function(event){
/*将所有行的选中状态设成全选框的选中状态*/
$tbr.find('input').prop('checked',$(this).prop('checked'));
/*并调整所有选中行的CSS样式*/
if ($(this).prop('checked')) {
$tbr.find('input').parent().parent().addClass('warning');
}
else{
$tbr.find('input').parent().parent().removeClass('warning');
}
getSum();
/*阻止向上冒泡,以防再次触发点击操作*/
event.stopPropagation();
});
/*点击全选框所在单元格时也触发全选框的点击操作*/
$checkAllTh.click(function(){
$(this).find('input:[type="checkbox"]').click();
});
var $tbr = $('table tbody tr');
var $checkItemTd = $('<td><input type="checkbox" name="checkItem" class="chk_itm"/></td>');
/*每一行都在最前面插入一个选中复选框的单元格*/
$tbr.prepend($checkItemTd);
/*点击每一行的选中复选框时*/
/*
$tbr.find('input').click(function(event){
//调整选中行的CSS样式
$(this).parent().parent().toggleClass('warning');
//如果已经被选中行的行数等于表格的数据行数,将全选框设为选中状态,否则设为未选中状态
$checkAll.prop('checked',$tbr.find('input:checked').length == $tbr.length ? true : false);
//阻止向上冒泡,以防再次触发点击操作
event.stopPropagation();
});
//点击每一行时也触发该行的选中操作
$tbr.click(function(){
$(this).find('input').click();
});
*/
}
initTableCheckbox();
getSum();
$(".chk_itm").change(function() {
getSum();
});
//添加商品
$("button[name='add']").click(function() {
let n = parseInt($(this).parent().siblings().find("input[name='num']").val());
console.log("添加商品的num:"+n);
n++;
$(this).parent().siblings().find("input[name='num']").val(n);
let price = $(this).parent().parent().parent().siblings(".price").html();
price = price.substr(1);
console.log("添加商品的price:"+price);
$(this).parent().parent().parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
getSum();
});
//减少商品
$("button[name='sub']").click(function() {
let n = parseInt($(this).parent().siblings().find("input[name='num']").val());
console.log("减少商品的num:"+n);
if (n === 1) {
return false;
}
n--;
$(this).parent().siblings().find("input[name='num']").val(n);
let price = $(this).parent().parent().parent().siblings(".price").html();
price = price.substr(1);
console.log("减少商品的price:"+price);
$(this).parent().parent().parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
getSum();
});
$("input[name='num']").change(function() {
let n = $(this).val();
console.log("数量num:"+n);
let price = $(this).parent().parent().parent().siblings(".price").html();
price = price.substr(1);
console.log("价格price:"+price);
$(this).parent().parent().parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
getSum();
});
function getSum() {
let count = 0; //计算总件数
let money = 0; //计算总价钱
$("input[name='num']").each(function(index) {
if ($(".chk_itm").eq(index).prop("checked") == true) {
count += parseInt($("input[name='num']").eq(index).val());
money += parseFloat($(".small_total").eq(index).text().substr(1));
}
});
$(".num_sum").html(count);
$(".sum").html(money.toFixed(2));
}
//点击删除之后一定是删除当前的商品,获取this
$("button[name='delete']").click(function() {
//删除的是当前的商品
$(this).parent().parent().remove();
$(".chk_itm").change();
getSum();
clearCheckAll();
});
//删除选定的商品:小的复选框如果选中就删除对应的商品
//删除的是选中的商品
$("button[name='delSome']").click(function() {
//删除的是选中的商品
$(".chk_itm:checked").parent().parent().remove();
getSum();
clearCheckAll();
});
//清空购物车
$("button[name='delAll']").click(function() {
$("#tby").empty();
getSum();
clearCheckAll();
});
function clearCheckAll() {
if ($("#tby").innerText == '') {
$(".checkAll").prop("checked", false);
}
}
});
效果
猜你喜欢
- 2024-09-11 浅析MySQL Join Reorder算法(mysqlinner join)
- 2024-09-11 js 小函数(js函数总结)
- 2024-09-11 Kubernetes 高性能网络组件 Calico 入门教程
- 2024-09-11 jQuery中的clone妙用(jquery.on)
- 2024-09-11 自定义一个"骚气"的jQuery
- 2024-09-11 前端单元测试以及自动化构建入门(前端单元测试是什么)
- 2024-09-11 Python全栈 Web(jQuery 一条龙服务)
- 2024-09-11 jQuery遍历说、详解与示例的结合,轻松搞定这个遍历!
- 2024-09-11 「clickhouse专栏」对标mongodb存储类JSON数据文档统计分析
- 2024-09-11 jQuery核心的3个面试问题(jquery前端面试)
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)