jQuery利用DOM遍历实现商城结算系统实战

 更新时间:2022年06月29日 14:55:44   作者:张阿起  
这篇文章主要介绍了jQuery利用DOM遍历实现商城结算系统实战,文章围绕主题展开详细的内容介绍,具一定的参考价值,感兴趣的小伙伴可以参考一下

效果图

一.body

<body>
		<div style="background-color: antiquewhite;width: 500px;height: 450px;border: 1px solid aquamarine;border-radius: 20px;">
			<table>
				<thead align="center">
					<p style="text-align: center;"><span style="font-size: 30px;color:mediumslateblue;border: 2px solid red;border-radius: 10px;background-color:navajowhite;">结算中心</span></p>
					<tr class="tr1">
						<th style="position: absolute;"><input type="checkbox" id="cballS">全选</th>
						<th  style="position: absolute;left: 100px;">商品</th>
						<th style="position: absolute;left: 190px;">单价</th>
						<th style="position: absolute;left: 290px;">数量</th>
						<th style="position: absolute;left: 370px;"><input type="hidden" value="0" />小计</th>
						<th style="position: absolute;left: 450px;">操作</th>
					</tr>
					<!--//作为换行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>

				</thead>

				<tbody>
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">选中</td>
						<td style="position: absolute;left: 100px;">vivoX86</td>
						<td style="position: absolute;left: 190px;">1299.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;" />
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)" >删除</a></td>
					</tr>
					<!--//作为换行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr class="tr1">
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">选中</td>
						<td style="position: absolute;left: 100px;">oppoS23</td>
						<td style="position: absolute;left: 190px;">1399.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">删除</a></td>
					</tr>
					<!--//作为换行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">选中</td>
						<td style="position: absolute;left: 100px;">HUAWEIS7</td>
						<td style="position: absolute;left: 190px;">1499.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">删除</a></td>
					</tr>
					<!--//作为换行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">选中</td>
						<td style="position: absolute;left: 100px;">Mi 8s</td>
						<td style="position: absolute;left: 190px;">1599.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">删除</a></td>
					</tr>
					<!--//作为换行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<td class="cbAll"><input type="checkbox" id="cball" onclick="chClick()">选中</td>
						<td style="position: absolute;left: 100px;">iPhone13</td>
						<td style="position: absolute;left: 190px;">1699.99</td>
						<td style="position: absolute;left: 260px;">
							<input type="button" value="-" style="background-color: aqua;border-radius: 10px;"/>
							<input type="text" value="0" style="width: 20px;text-align: center;border-radius: 10px;background-color: gold;"/>
							<input type="button" value="+" style="background-color: blueviolet;border-radius: 10px;"/>
						</td>
						<td style="position: absolute;left: 370px;border: 1px solid chartreuse;border-radius: 10px;background-color: salmon;width: 60px;text-align: center;">0</td>
						<td style="position: absolute;left: 450px;"><a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  onclick="del(this)">删除</a></td>
					</tr>
				</tbody>
				<tfoot>
					<!--//作为换行使用-->
					<tr>
						<td ><p>&nbsp;</p></td>
					</tr>
					
					<tr>
						<th><span style="background-color:aqua;border:1px solid darkblue;border-radius: 5px;display:inline-block;width: 80px;">总金额</span>&nbsp;:&emsp;<span id="moneys" style="display:inline-block;width: 100px;background-color: seagreen;border-radius: 10px;">0</span>元</th>
						<th>&emsp;<a href="#" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >结算</a></th>
					</tr>
				</tfoot>
			</table>
		</div>
	</body>

二.css

<style>
			.cbAll{
				border: 1px solid darkblue;
				background-color: darkgoldenrod;
				border-radius: 10px;
				position:absolute;
				
			}
		</style>

三.JavaScript

<script type="text/javascript" src="js/jquery.min.js" ></script>
		<script>
			$(function(){
				$("[type=button]").click(function(){
					//获取按钮
					var btn=$(this).val();
					//获取父元素再获取父元素的上一个同胞元素
					var Fu=parseFloat($(this).parent().prev().html());
					//获取父元素后获取父元素下一个同胞元素
					//var FuX=parseFloat($(this).parent().next().html());
					if(btn=="+"){
						//获取数量
						var preVa=parseInt($(this).prev().val());
						//给数量加1
						$(this).prev().val(preVa+1)
						//获取并赋值数量
						preVa=parseInt($(this).prev().val());
						//给父元素下一个同胞元素赋值,并保留小数点后两位
						var ptsum=(Fu*preVa).toString();
						var newSum="";
						for(i=0;i<ptsum.length;i++){
							if(ptsum[i]=="."){
								if(ptsum[i+1]!=undefined){
									newSum+=ptsum[i]+ptsum[i+1];
								}
								if(ptsum[i+2]!=undefined){
									newSum+=ptsum[i+2];
								}
								break;
							}
							newSum+=ptsum[i];
						};
						$(this).parent().next().html(newSum);
						Smon();
						
					}else{
						//获取数量
						var preVa=parseInt($(this).next().val());
						if(preVa==0){
							return alert("不能再少了!");
						}
						//给数量减一
						$(this).next().val(preVa-1);
						//获取并赋值元素
						preVa=parseInt($(this).next().val());
						//给父元素下一个同胞元素赋值,并保留小数点后两位
						var ptsum=(preVa*Fu).toString();
						var newSum="";
						for(i=0;i<ptsum.length;i++){
							if(ptsum[i]=="."){
								if(ptsum[i+1]!=undefined){
									newSum+=ptsum[i]+ptsum[i+1];
								}
								if(ptsum[i+2]!=undefined){
									newSum+=ptsum[i+2];
								}
								break;
							}
							newSum+=ptsum[i];
						}
						$(this).parent().next().html(newSum);
						Smon();
						
					};
					
				});
				
				//全选按钮
				$("#cballS").click(function(){
					
					var flag=$(this).prop("checked");
					$("[type=checkbox]").each(function(){
						$(this).prop("checked",flag);
						
					});
					Smon()
				});
				//输入框发生变化刷新小计
				
				$("[type='text']").change(function(){
							
							//获取元素内容
							var ptNum=parseFloat($(this).val());
							
							if(ptNum<=0 || isNaN(ptNum)){
								$(this).val(0);
								return false;
							};
							var n1=ptNum.toString();
							for(i=0;i<n1.length;i++){
								//不能为小数或者不能大于100
								if(n1[i]=="." || i==2){
									$(this).val(0);
									return false;
								};
							};
							
							//获取单价
							var ptDj=parseFloat($(this).parent().prev().html());
							//获取总额
							var ptSum=(ptNum*ptDj).toString();
							var newSum="";
							//保留小数点后两位
							for(i=0;i<ptSum.length;i++){
								if(ptSum[i]=="."){
									if(!isNaN(ptSum[i+1])){
										newSum+=ptSum[i]+ptSum[i+1];
										if(!isNaN(ptSum[i+2])){
											newSum+=ptSum[i+2];
										};
										break;
									};

								};
								newSum+=ptSum[i];
							}
							//设置小计
							$(this).parent().next().html(newSum);
							Smon();
				});
				
				
			});
			//计算总金额
			function Smon(){
					var sum=0;
					//循环遍历所有选中按钮后边的值
					$("[type=checkbox]:checked").each(function(){
						//获取选中按钮同级的小计
						var xiaoji=parseFloat($(this).parent().next().next().next().next().html());
						//判断是否是第一行的,isNaN判断是否可以转数值
						if(isNaN(xiaoji)){
							xiaoji=0;
						}
						sum+=xiaoji;
					});
					sum=parseFloat(sum).toString();
					var newSum="";
					//总计金额默认保存小数点后两位,循环拼接并赋值给新的变量
					for(i=0;i<sum.length;i++){
						if(i<=7){
							if(sum[i]=="."){
								if(sum[i+1]!=undefined){
									newSum+=sum[i]+sum[i+1];
								}
								if(sum[i+2]!=undefined){
									newSum+=sum[i+2];
								}
								break;
							}
							newSum+=sum[i];
						}
					}
					//赋值
					$("#moneys").html(newSum);
			};
			//选中按钮判断
			function chClick(){
					//获取全选按钮
					var len=$("#cballS").prop("checked");
					//获取所有可选择的按钮为true的长度
					var len2=$("[type=checkbox]:checked").length;
					//获取所有可选中按钮的长度
					var len3=$("[type=checkbox]").length;
					//由于函数执行是按照自上而下,调用一次执行一次的方式,所以当第一次判断选中按钮为5个时,就去勾选全选按钮,
					//当以后每次判断再进来时,只会有两种结果,一个全选是true,一个全选是false,只需要判断全选是什么状态就将状态改变另一个即可。
					if(len2==len3-1 & len==false){
						$("#cballS").prop("checked",true);
					}else if(len2>=len3-1 & len==true){
						$("#cballS").prop("checked",false);
					};
					//执行完调用总计函数
					Smon();
			};
			
			//删除,使用函数调用完成
			function del(th){
				var aa=$(th).parent().parent().next().remove();
				$(th).parent().parent().remove();
				Smon();
			};
		
		</script>

四.小结

本章使用所学jQueryDOM遍历实现了一个实用简易小商城结算系统,实现方式不只这一种,各位感兴趣的小伙伴,可以自行探索.

到此这篇关于jQuery利用DOM遍历实现商城结算系统实战的文章就介绍到这了,更多相关jQuery DOM遍历 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • jQuery操作文本方法介绍

    jQuery操作文本方法介绍

    这篇文章介绍了jQuery操作文本的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • jquery实现简单易懂的图片展示小例子

    jquery实现简单易懂的图片展示小例子

    图片展示想必大家都见到过吧,下面有个不错的例子,通俗易懂,使用jquery实现的,感兴趣的朋友不要错过
    2013-11-11
  • jQuery validate(submitHandler函数)验证通过发送Ajax(实例详解)

    jQuery validate(submitHandler函数)验证通过发送Ajax(实例详解)

    这篇文章主要介绍了jQuery validate(submitHandler函数)验证通过发送Ajax,可以用validate框架验证元素,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2022-10-10
  • jQuery控制Div拖拽效果完整实例分析

    jQuery控制Div拖拽效果完整实例分析

    这篇文章主要介绍了jQuery控制Div拖拽效果完整实例,对jQuery控制拖拽效果的方法以注释形式给予了较为详细的介绍,便于读者理解代码含义,需要的朋友可以参考下
    2015-04-04
  • jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析

    jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析

    这篇文章主要介绍了jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较,结合实例形式分析了jQuery事件模型默认行为执行顺序相关原理,以及trigger()与 triggerHandler()的异同点比较,需要的朋友可以参考下
    2020-04-04
  • jquery分割字符串的方法

    jquery分割字符串的方法

    这篇文章主要介绍了jquery分割字符串的方法,涉及jQuery中使用split方法进行字符串分割的相关技巧,需要的朋友可以参考下
    2015-06-06
  • jQuery中queue()方法用法实例

    jQuery中queue()方法用法实例

    这篇文章主要介绍了jQuery中queue()方法用法,实例分析了queue()方法的功能、定义及使用技巧,非常具有实用价值,需要的朋友可以参考下
    2014-12-12
  • jquery在项目中做复选框时遇到的一些问题笔记

    jquery在项目中做复选框时遇到的一些问题笔记

    在实践中还是遇到了很多的问题,注意在input的checkbox中,用普通的attr属性来判断是不可以的,因为checked的值是checked,因此做个笔记
    2013-11-11
  • jquery tools系列 overlay 学习

    jquery tools系列 overlay 学习

    接着上次scrollable的学习,今天继续jquery tools六大功能的第四个功能——overlay的学习。
    2009-09-09
  • 简单常用的幻灯片播放实现代码

    简单常用的幻灯片播放实现代码

    幻灯片自动播放图片是当前网站比较流行的一个展示方式,这里项目需要,我自己做了一个简单的,就不详细讲解了,代码很简单。直接看效果图和代码吧,希望对大家有所帮助
    2013-09-09

最新评论