Web开发基础_JQuery学习_0009_jQuery版本 购物车案例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Coder_Boy_/article/details/81952372

jQuery版本 购物车案例

案例演示:

需要引入 jquery-1.11.1.js

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>购物车</title>
		<link rel="stylesheet" href="wcss.css" type="text/css" />
		<!-- 在head放置<link rel="stylesheet" href="wcss.css" type="text/css" />
		来调用外部的wcss.css文件来实现html引用css文件 -->
		<style type="text/css">
			h1 {
				text-align:center;
			}
			
			table {
				margin:0 auto;
				width:60%;
				border:2px solid #aaa;
				border-collapse:collapse;
			}
			
			table th,table td {
				border:2px solid #aaa;
				padding:5px;
			}
			
			th {
				background-color:#eee;
			}
		</style>
		<script type="text/javascript" src="../js/jquery-1.11.1.js"></script>
		<script type="text/javascript">
			//加入购物车
			function add_shoppingcart(btn){
				//获取按钮父亲的兄弟(tds)
				var $tds = $(btn).parent().siblings();
				//$obj.eq(i) == $($obj[i])
				//获取第一个td的内容(商品名)
				//var name =  $($tds[0]).html();
				//var name = $tds[0].innerHTML;
				var name = $tds.eq(0).html();
				//获取第二个td的内容(单价)
				var price = $tds.eq(1).html();
				//测试
				console.log(name+","+price);
				
				//创建一个新行
				var $tr = 
					$('<tr>'+
					  '<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>');
				//在tbody下追加此行
				$("#goods").append($tr);
				
				sum();
			}
			
	    	//加法
	    	function increase(btn) {
	    		//获取按钮的哥哥的值(数量)
	    		var amount = $(btn).prev().val();
	    		//数量+1再写入按钮的哥哥内
	    		$(btn).prev().val(++amount);
	    		//获取按钮父亲的哥哥的内容(单价)
	    		var price = $(btn).parent().prev().html();
	    		//计算金额并写入按钮父亲的弟弟内
	    		$(btn).parent().next().html(amount*price);
	    		
	    		sum();
	    	}
			
	    	//减法
	    	function decrease(btn) {
	    		//获取按钮的弟弟的值(数量)
	    		var amount = $(btn).next().val();
				if(amount<=0){
	    			return;
	    		}	
	    		//数量-1再写入按钮的弟弟内
	    		$(btn).next().val(--amount);
	    		//获取按钮父亲的哥哥的内容(单价)
	    		var price = $(btn).parent().prev().html();
	    		//计算金额并写入按钮父亲的弟弟内
	    		$(btn).parent().next().html(amount*price);
	    		
	    		sum();
	    	}
	    	
	    	
		    //删除商品
		   function del(btn){
			  //找到 tr
			  $tr = $(btn).parent().parent();
			  //移除商品行记录
			  $tr.remove();
			  sum();
		   }
	    	
	    	//合计
	    	function sum() {
	    		//获取tbody下所有的行
	    		var $trs = $("#goods tr");
	    		//遍历这些行
	    		var sum = 0;
	    		for(var i=0;i<$trs.length;i++) {
	    			//获取本行第4列的内容(金额)
	    			var mny = $trs.eq(i).children().eq(3).html();
	    			//累加金额
	    			sum += parseFloat(mny);
	    		}
	    		//将合计值写入合计列
	    		$("#total").html(sum);
	    	}
		</script>
	</head>
	<body>
		<h1>好划算</h1>
		<table>
			<tr>
				<th>商品</th>
				<th>单价</th>
				<th>颜色</th>
				<th>库存</th>
				<th>好评率</th>
				<th>操作</th>
			</tr>
			<tr>
				<td>罗技M185鼠标</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>微软X470键盘</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>金士顿U盘</td>
				<td>70</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>商品</th>
					<th>单价(元)</th>
					<th>数量</th>
					<th>金额(元)</th>
					<th>删除</th>
				</tr>
			</thead>
			<tbody id="goods">
				<!-- <tr>
					<td>罗技MT185鼠标</td>
					<td>80</td>
					<td align="center">
						<input type="button" value="-">
						<input type="text" size="3" readonly value="1">
						<input type="button" value="+" onclick="increase(this)">
					</td>
					<td>80</td>
					<td align="center"><input type="button" value="x"></td>
				</tr> -->
			</tbody>
			<tfoot>
				<tr>
					<td colspan="3" align="right">总计</td>
					<td id="total"></td>
					<td></td>
				</tr>
			</tfoot>
			
		</table>
	</body>
</html>

最终页面显示效果:

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/81952372