<!DOCTYPE html>
<html>
<head>
<title> 购物车 </title>
<meta charset="utf-8" />
<style type="text/CSS">
h1 {
text-align:center;
}
table {
margin:0 auto;
width:40%;
border:2px solid #aaa;
border-collapse:collapse;
}
table th, table td {
border:2px solid #aaa;
padding:5px;
}
th {
background-color:#eee;
}
a:hover {
color: #e33333;
}
a {
color: #666;
text-decoration: none;
}
</style>
<script src="../js/jquery-3.3.1.js"></script>
<script>
function checkAll(){
if($(".j-checkbox:checked").length===$('.j-checkbox').length){
$(".checkall").prop("checked",true);
}else{
$(".checkall").prop("checked",false);
}
}
function add_shoppingcart(btn){
// 获取商品名
var name = $(btn).parent().siblings().eq(0).html();
// 获取单价
var price = $(btn).parent().siblings().eq(1).html();
// 拼一行
var tr ="<tr class='cart-item'>"+
'<td><input type="checkbox" class="j-checkbox" onclick="checkAll(this)"></td>' +
'<td>' + name + '</td>' +
'<td>' + price + '</td>' +
'<td align="center">' +
'<input type="button"value="-"onclick="decrease(this);"/>' +
'<input type="text"size="3"readonly value="1"/>' +
'<input type="button"value="+"onclick="increase(this);"/>' +
'</td>' +
'<td>' + price + '</td>' +
'<td align="center"><input type="button"value="x"onclick="del(this);"/></td>' +
'</tr>';
// 追加此行
$("#goods").append($(tr));
sum();
checkAll();
}
// 加入购物车
/* function add_shoppingcart(btn) {
//obj.eq(i)== $(obj[i])
// 获取商品名
var name = $(btn).parent().siblings().eq(0).html();
// 获取单价
var price = $(btn).parent().siblings().eq(1).html();
// 拼一行
var tr =
'<tr class="cart-item">' +
'<td><input type="checkbox" class="j-checkbox"></td>' +
'<td>' + name + '</td>' +
'<td>' + price + '</td>' +
'<td align="center">' +
'<input type="button"value="-"onclick="decrease(this);"/>' +
'<input type="text"size="3"readonly value="1"/>' +
'<input type="button"value="+"onclick="increase(this);"/>' +
'</td>' +
'<td>' + price + '</td>' +
'<td align="center"><input type="button"value="x"onclick="del(this);"/></td>' +
'</tr>';
// 追加此行
$("#goods").append($(tr));
sum();
} */
function del(btn) {
var s = $(btn).closest("tr");
$(s).remove();
sum();
}
//删除按钮
/* function del(btn) {
var s = $(btn).parent().parent();
$(s).remove();
sum();
} */
// (2) 删除选中的商品
$(function() {
// (2) 删除选中的商品
$(".remove-batch").click(function() {
// 删除的是小的复选框选中的商品
$(".j-checkbox:checked").parents(".cart-item").remove();
sum();
});
// (3) 清空购物车 删除全部商品
$(".clear-all").click(function() {
$(".cart-item").remove();
sum();
})
//购物车全选,当按下全选后,其他子项会被选中,当所有子项被选中后,全选也会选中
$('.checkall').change(function(){
//console.log($(this).prop('checked'))
//alert("hello");
$('.j-checkbox').prop("checked",$(this).prop('checked'));
})
// $(".cart-item .j-checkbox").change(function(){
// //console.log($(this).prop('checked'))
// alert("hello");
// /* if($(".j-checkbox:checked").length===$('.j-checkbox').length){
// $(".checkall").prop("checked",true);
// }else{
// $(".checkall").prop("checked",false);
// } */
// })
});
//删除选中的
// 求和
function sum() {
// 获取 tbody 下所有的行
var $trs = $("#goods tr");
console.log($trs);
console.log($trs.length);
// 遍历他们
var sum = 0;
for (var i = 0; i < $trs.length; i++) {
// 获取每一行
var $tr = $trs.eq(i);
console.log($tr);
// 获取该行中第 4 列的值 (金额 / String)
var mny =
$tr.children().eq(4).html();
// tds td3 金额
console.log(mny);
sum += parseFloat(mny);
}
console.log(sum);
// 将合计值写入合计列
$("#total").html(sum);
};
// 加法
function increase(btn){
// 获取数量
var n=$(btn).prev().val();
//+1, 再写入框内
$(btn).prev().val(++n);
// 获取单价
var p=$(btn).parent().prev().html();
// 计算金额并写入金额列
$(btn).parent().next().html(p*n);
sum();
}
// 减法
function decrease(btn){
var n=$(btn).next().val();
// 数量不能小于 1
if(n<= 1) {
return;
}
$(btn).next().val(--n);
var p=$(btn).parent().prev().html();
$(btn).parent().next().html(p*n);
sum();
}
</script>
</head>
<body>
<h1> 巨划算 </h1>
<table>
<tr>
<th> 商品 </th>
<th> 单价 (元)</th>
<th> 颜色 </th>
<th> 库存 </th>
<th> 好评率 </th>
<th> 操作 </th>
</tr>
<tr>
<td> 女士小皮包 </td>
<td>80</td>
<td> 黑色 </td>
<td>893</td>
<td>98%</td>
<td align="center">
<input type="button" value="加入购物车" onclick="add_shoppingcart(this);" />
</td>
</tr>
<tr>
<td> 夏日爆款松糕鞋 </td>
<td>150</td>
<td> 白色 </td>
<td>9028</td>
<td>96%</td>
<td align="center">
<input type="button" value="加入购物车" onclick="add_shoppingcart(this);" />
</td>
</tr>
<tr>
<td>iphone6 手机壳 </td>
<td>60</td>
<td> 透明 </td>
<td>672</td>
<td>99%</td>
<td align="center">
<input type="button" value="加入购物车" onclick="add_shoppingcart(this);" />
</td>
</tr>
<tr>
<td> 无线鼠标 </td>
<td>100</td>
<td> 蓝色 </td>
<td>8937</td>
<td>95%</td>
<td align="center">
<input type="button" value="加入购物车" onclick="add_shoppingcart(this);" />
</td>
</tr>
<tr>
<td> 三叶草新款运动鞋 </td>
<td>670</td>
<td> 红色 </td>
<td>482</td>
<td>100%</td>
<td align="center">
<input type="button" value="加入购物车" onclick="add_shoppingcart(this);" />
</td>
</tr>
</table>
<h1> 购物车 </h1>
<table>
<thead>
<tr>
<th><input type="checkbox" class="checkall"></th>
<th> 商品 </th>
<th> 单价 (元)</th>
<th> 数量 </th>
<th> 金额 (元)</th>
<th> 删除 </th>
</tr>
</thead>
<tbody id="goods">
</tbody>
<tfoot>
<tr>
<td colspan="2" align="center"><a href="javascript:;" class="remove-batch"> 删除选中的商品;</a>
<a href="javascript:;" class="clear-all">清理购物车</a>
</td>
<td align="right">总计</td>
<td id="total">0</td>
<td></td>
</tr>
</tfoot>
</table>
</body>
</html>
唐俊-jQuery备课代码.zip
需积分: 0 134 浏览量
2022-12-12
18:00:44
上传
评论
收藏 518KB ZIP 举报
2201_75822807
- 粉丝: 0
- 资源: 1
最新资源
- 基于Java和Javascript的音乐悬浮歌词设计源码 - musicLrc
- ripgrep tools
- kubesphere离线安装v3.3的centos7iso资源
- xx 文库下载神器,目前亲测试pdf 有效
- kubesphere离线安装v3.3的v3.0.7的kk资源
- kubesphere离线安装v3.3的ubuntu2020iso资源
- 一个简单的Python脚本,它包含了将点分十进制IP地址转换为32位整数(通常称为IPv4地址的整数表示)和进行简单的IP地址校
- scratch2源码星球大战
- Java springboot 创建springboot项目基础框架
- 成绩管理系统后台20240529135001
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈