用jQuery做一个简单的购物车

用jQuery做一个简单的购物车

前言:

之前用的JavaScript做的,现在结合jQuery再试一下。效果虽然差不多,但是对于了解和学习jQuery选择器还有事件等可以有一个更好的了解。

(img)自行准备,部分代码优化,大佬多指教


关于循环可以用jQuery里面的each

实现功能:
  • 将商品添加到购物车(如果购物车有相同物品则累加对应数量)
  • 小计计算与总和
  • 积分计算与总和
  • 实现数量的增加和减少(并同步小计和积分及其总和)
  • 单个商品删除
  • 批量删除
实现效果:

在这里插入图片描述

CSS代码:
#info-table{
   text-align: center;}
#info-input{
   width: 1200px;margin: 0px auto;}
#info-input>div{
   
	width: 1200px;
	margin: 20px 0px;
}
.shopCount{
   color: orange;}
a{
   text-decoration: none;color: deepskyblue;}
#resultTotalMoney,#integralTotal{
   color: orange;}
.total-div{
   text-align: right;}
.btdelete{
   float: left;}
.btorinter-div{
   height: auto;overflow: auto;}
.viewIntegral{
   float: right;}
.btbuy{
   background-color: orange;color: white;border: 0px;float: right;}
#shop{
   
	width: 800px;margin: 0px auto;
	height: auto;
	overflow: auto;
}
#shop li{
   
	text-align: center;
	list-style: none;
	float: left;
	height: auto;
	overflow: auto;
	margin: 20px;
}
#shop a{
   
	display: block;
	height: auto;
	overflow: auto;
}
.price{
   color: red;}
HTML+jQuery代码:
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>jQuery实现购物车</title>
	</head>
	<link rel="stylesheet" type="text/css" href="css/mycart.css"/>
	<script src="js/jquery-3.2.1.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		$(function(){
    
			// 全选
			$("#allCheck").click(function(){
    
				$(".selectOne").prop("checked",$(this).prop("checked"));
			})
			
			// 单个删除
			$("#mytable").on("click",".delete",function(){
    
				if (confirm("是否将该物品移除购物车?")){
    
					$(this).parents("tr").remove();
					// 刷新总价
					allShopPriceTotal();
				}
			})
			
			// 批量删除
			$(".btdelete").on("click",function(){
    
				if (confirm("是否从购物车移除选择的所有商品?")){
    
					$(".selectOne:checked").parents("tr").remove();
					// 刷新总价
					allShopPriceTotal()
  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A: HTML代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>京东购物车</title> <link href="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js" rel="stylesheet"> </head> <body> <table id="cart"> <thead> <tr> <th>商品</th> <th>单价</th> <th>数量</th> <th>小计</th> <th>操作</th> </tr> </thead> <tbody> <tr> <td>小米电视</td> <td class="price">1999</td> <td><input class="num" type="number" value="1" min="1"></td> <td class="subtotal"></td> <td><button class="delete">删除</button></td> </tr> <tr> <td>华为手机</td> <td class="price">3999</td> <td><input class="num" type="number" value="1" min="1"></td> <td class="subtotal"></td> <td><button class="delete">删除</button></td> </tr> <tr> <td>华硕笔记本</td> <td class="price">5999</td> <td><input class="num" type="number" value="1" min="1"></td> <td class="subtotal"></td> <td><button class="delete">删除</button></td> </tr> </tbody> <tfoot> <tr> <td colspan="3"></td> <td id="total"></td> <td><button id="clear">清空购物车</button></td> </tr> </tfoot> </table> <script src="cart.js"></script> </body> </html> ``` JavaScript代码: ```javascript $(function() { // 计算小计和总价 function calculate() { // 遍历所有商品行 $("#cart tbody tr").each(function() { // 获取单价、数量和小计元素 var priceEl = $(this).find(".price"); var numEl = $(this).find(".num"); var subtotalEl = $(this).find(".subtotal"); // 计算小计 var price = parseFloat(priceEl.text()); var num = parseInt(numEl.val()); var subtotal = price * num; // 更新小计元素 subtotalEl.text(subtotal.toFixed(2)); }); // 计算总价 var total = 0; $(".subtotal").each(function() { total += parseFloat($(this).text()); }); // 更新总价元素 $("#total").text(total.toFixed(2)); } // 绑定数量输入框的change事件 $(".num").on("change", function() { calculate(); }); // 绑定删除按钮的click事件 $(".delete").on("click", function() { $(this).parents("tr").remove(); calculate(); }); // 绑定清空购物车按钮的click事件 $("#clear").on("click", function() { $("#cart tbody").empty(); calculate(); }); // 初始化小计和总价 calculate(); }); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值