本文介绍了计算表格中的运费的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在 http:// www。 develop.check.com.au/hh/order.html 其中 一个扩展小计,然后计算一个总计 a数量由用户输入。我希望能够将 运费加到该总额中。 运费在邮资和邮件中处理费用链接,每箱价格为。如果有人选择美国,加拿大,中东,从 下拉框中它应该重新计算运费,否则它将在默认情况下计算。澳大利亚。 如果我在这里发布计算函数,它们将会换行,但它们位于 http://www.develop.check.com.au/hh/calculate2.js 很多TIA的帮助。 解决方案 计算: shipping = new Array(''5.00'',''7.50'',''8.50'',''9.50''); function total2(what){ var quantTotal = 0; var j = 0; for(var i = 0; i< what.length; i ++) if(what.elements [i] .name.substring(0,4)==''quant'') j ++; for(var i = 0; i< j; i ++) quantTotal + = what.element s [''quantity''+ i] .value * 1; shippingCost = quantTotal * 运费[what.elements [''location'']。selectedIndex] ; shippingCost = quantTotal> 200? shippingCost * 0.8: quantTotal> 100? shippingCost * 0.9: quantTotal> 50? shippingCost * 0.95: shippingCost * 1; what.grandTotal.value + = shippingCost; what.grandTotal.value = roundoff(Math .round(what.gra ndTotal.value * Math.pow(10,2))/ Math.pow(10,2)); } - Stephane Moriaux et son [moins] vieux Mac 计算caculate2.js : grandTotal + =(what.elements [''price''+ i] .value - 0)* (what.elements [''quantity''+ i] .value - 0); 在 乘法之前,无需将String显式转换为数字。 quantTotal + = what。元素[ '' 量 '' + I]。价值* 1; 无需乘以1即可将String转换为Number;一元一天+ 更优雅。 见常见问题。 小计=舍入(Math.round(小计*数学) .pow(10,2))/ Math.pow(10,2)); what.grandTotal.value = 舍入(Math.round(grandTotal * Math.pow(10,2)) /Math.pow(10,2)); Math.pow(10,2)*总是* 100;没有必要计算每个 时间,甚至一次。并且没有定义舍入。 - ?约翰斯托克顿,英国萨里。 [email protected] Turnpike v4.00 IE 4? < URL:http://www.jibbering.com/faq/> JL / RC:新闻常见问题:comp.lang.javascript < URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr数学,日期,来源。 < URL:http://www.merlyn.demon.co.uk/> TP / BP / Delphi / jscr /& c,FAQ项目,链接。 谁可以做更多可以做得更少:-) 无需乘以1即可将String转换为Number;一元吧+ 更优雅。 不明白什么是一元+? foo = foo * 1 foo = foo + 0 或 foo = Number(foo); 看到任何比其他更好(或优雅) 见常见问题。 如果我找回它 Math .pow(10,2)总是* 100;没有必要每隔一次计算一次,甚至一次。并且没有定义舍入。 我确实认为这不是我的代码被剥夺了... 并认为这是一个黑客 如果是着名的JS轮数= 9.000000000002 - Stephane Moriaux et son [moins] vieux Mac I have a form at http://www.develop.check.com.au/hh/order.html wherean extended sub-total and then a grand total is calculated as soon asa quantity is input by a user. I would like to be able to add theshipping cost to that grand total. Shipping costs are in the "Postage & handling charges" link and are acost per box. If somebody selects "USA, Canada, Middle East" from thedrop down box it should recalculate the shipping cost, otherwise itshould calculate on the default - Australia. If I post the calculation functions here they''ll wrap but they are at http://www.develop.check.com.au/hh/calculate2.js Many TIA for your help. 解决方案 calculation here : shipping = new Array(''5.00'',''7.50'',''8.50'',''9.50''); function total2(what) {var quantTotal = 0;var j=0;for(var i=0;i<what.length;i++)if(what.elements[i].name.substring(0,4)==''quant'')j++;for (var i=0;i<j;i++)quantTotal += what.elements[''quantity''+i].value*1;shippingCost = quantTotal *shipping[what.elements[''location''].selectedIndex];shippingCost = quantTotal>200? shippingCost*0.8 :quantTotal>100? shippingCost*0.9 :quantTotal>50? shippingCost*0.95 :shippingCost*1;what.grandTotal.value += shippingCost;what.grandTotal.value=roundoff(Math.round(what.gra ndTotal.value*Math.pow(10,2))/Math.pow(10,2));}--Stephane Moriaux et son [moins] vieux Maccalculation in caculate2.js :grandTotal += (what.elements[''price'' + i].value - 0) *(what.elements[''quantity'' + i].value - 0);There is no need to explicitly convert String to Number beforemultiplication. quantTotal += what.elements[''quantity''+i].value*1;There is no need to multiply by 1 to convert String to Number; a unary +does it more elegantly.See FAQ. subtotal = roundoff(Math.round(subtotal*Math.pow(10,2))/Math.pow(10,2));what.grandTotal.value =roundoff(Math.round(grandTotal*Math.pow(10,2))/Math.pow(10,2)); Math.pow(10, 2) is *always* 100; there is no need to calculate it everytime, or even once. And roundoff is not defined. --? John Stockton, Surrey, UK. [email protected] Turnpike v4.00 IE 4 ?<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. who can do more can do less :-) There is no need to multiply by 1 to convert String to Number; a unary + does it more elegantly. not understood what is a unary + ? foo = foo*1foo = foo+0orfoo = Number(foo); see any really better (or elegant) than other one See FAQ. if I find it back Math.pow(10, 2) is *always* 100; there is no need to calculate it every time, or even once. And roundoff is not defined. I did think this not mine code was debuged ...and thought it was a hackin case of famous JS round number = 9.000000000002 --Stephane Moriaux et son [moins] vieux Mac 这篇关于计算表格中的运费的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-20 22:07