js合计,不能自动求和,请教大神
<!DOCTYPEhtml><html><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"conte...
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>JS计算</title> <link rel="stylesheet" href=""> <script src="jquery-3.2.1.min.js"></script> <style> table , tr ,th,td{ border: 1px solid #ccc; border-collapse: collapse; text-align: center; } a{ height: 25px; width: 25px; line-height: 25px; text-align: center; display: inline-block; border:1px solid #ccc; text-decoration: none; } .right{ text-align: right; padding-right:15px; } </style></head><body> <table> <thead> <tr> <th width="5%;">ID</th> <th width="27%;">品名</th> <th width="27%;">规格</th> <th width="8%;">单价</th> <th width="6%;">优惠</th> <th width="7%;">数量</th> <th width="10%;">小计</th> <th width="8%;">新增</th> </tr> </thead> <tbody> <tr class="Spoint"> <td>1</td> <td>品名</td> <td>规格</td> <td>1050</td> <td>0.63</td> <td>2</td> <td class="Splan">2000</td> <td><a href="javascipt:void(0);" onclick="newTd(this);">+</a></td> </tr> <tr class="Total"> <td colspan="8" class="right">1050</td> </tr> </tbody> </table> <script> /*新增*/ function newTd(e){ var tr = $(e).parent().parent();/*内容*/ var pr = $(e).parent().parent();/*位置*/ var $Spoint = $(e).parents(".Spoint"); var $Splan = $(e).parents().find(".Splan"); var $Total = $(e).parents(".Spoint").parent().find(".right"); var newtr = tr.clone(true,true); newtr.find("a").html("-"); //console.log(newtr); /*console.log($Spoint.html()); console.log($Splan.html()); console.log($Total.html());*/ if ($(e).html() === "-") { pr.remove(); total_menoy($Spoint,$Splan,$Total); } else { pr.after(newtr); total_menoy($Spoint,$Splan,$Total); } } /*合计*/ function total_menoy($Spoint,$Splan,$Total){ var hj = 0; $Spoint.each(function() { var xj = RetainedDecimalPlaces($Spoint.find($Splan).html(),0); hj += xj; }); $Total.html(hj); } /*数字处理*/ function RetainedDecimalPlaces(num,del)/*值:num 小数位:del*/{ if (del != 0) { /*函数保留小数并四舍五入*/ num = parseFloat(num).toFixed(del); }else{ num = parseInt(num); } return num; }; </script></body></html>
展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询