zoukankan      html  css  js  c++  java
  • JavaScript自动计算并且保留两位小数

    页面使用模板引擎velocity,要JavaScript实现再一个输入框中输入值,另一个输入框中根据输入的值来进行计算,得到结果。同时对结果保留两位小数。
    下面是我项目中用的源代码。
    此链接是关于该方法的详细讲解
    http://www.w3school.com.cn/jsref/jsref_tofixed.asp

    <p>#set($path="") ## 标题:创建空交接单 ## @author: zhangbbj ## @time:2016.7.5 //运代交接-新增交接单 按钮修改 2016.05.05</p> <script type="text/javascript" src="$!{path}/resources/tms/js/consignee.js"></script> <div class="easyui-layout" data-options="fit:true"> <div data-options="region:'north'"> <table class="tms_tool" style="height: 23px; 326px;"> <tbody> <tr> <td class="tool_btn_right"><input id="autoPrintFlag" type="checkbox" checked="checked" /><label for="autoPrintFlag">自动打印</label> <a id="add_confirm_btn" href="#">确认</a></td> </tr> </tbody> </table> </div> <div id="add_center" style="padding: 0;" data-options="region:'center',fit:false"><form id="addDispatchPaperForm" method="POST" enctype="multipart/form-data"><input id="jsonData" type="hidden" name="jsonData" /> <input id="autoPrint" type="hidden" name="autoPrint" value="0" /> <input id="add_chargeType" type="hidden" name="chargeType" value="0" /> <table class="table-normal width-99p table-border-1s-gray table-th-center table-td-center table-th-bgcolor-1"> <tbody> <tr id="baoxian4"><th>输入数字</th> <td><input id="add_insurance_money" style=" 98%;" type="text" name="insuranceMoney" onchange="countMoneyForInsurance();" /></td> <th>计算结果</th> <td><input id="add_insurance_money_give" style=" 98%;" type="text" name="insuranceMoneyGive" /></td> </tr> </tr> </tbody> </table> </form></div> </div> <script type="text/javascript" src="$!{path}/resources/jQuery/js/jquery.form.js"></script> <script type="text/javascript">// <![CDATA[ //根据输入的数字计算出结果并自动填充在输入框中。 function countMoneyForInsurance() { //方法一使用id选择器进行计算。 //获取输入的数字 var money = $("#add_insurance_money").val(); //自动计算出结果并填充在相应的输入框中 $("#add_insurance_money_give").val((money*8/10000).toFixed(2)); //方法二使用document进行计算。 // var consigneeName= document.getElementById("add_insurance_money").value; // var countMoney = consigneeName*8/10000; // // document.getElementById("add_insurance_money_give").value = countMoney; } // ]]></script>





    这是一个简单的小例子
    <html>
    <head>
    <script type="text/javascript">
    function displaynum()
    {
    var num = new Number(18.86);
    var num1 = new Number(18.33); alert(num.toFixed(1));
    alert(num1.toFixed(1)); } </script> </head> <body> Show the number 13.37 with one decimal: <form> <input type="button" value="Show!" onclick="displaynum()" > </form> </body> </html>



  • 相关阅读:
    (原创)c#学习笔记10--定义类成员01--成员定义06--自动属性
    (原创)c#学习笔记10--定义类成员01--成员定义03--定义属性
    (原创)c#学习笔记10--定义类成员01--成员定义02--建立方法
    【BZOJ3110】K大数查询(ZJOI2013)-整体二分+线段树
    【BZOJ3110】K大数查询(ZJOI2013)-整体二分+线段树
    【HDU5412】CRB and Queries-整体二分:带修改区间第K小
    【HDU5412】CRB and Queries-整体二分:带修改区间第K小
    【NOI2007T2】货币兑换-DP斜率优化+CDQ分治
    【NOI2007T2】货币兑换-DP斜率优化+CDQ分治
    【BZOJ3295】动态逆序对(CQOI2011)-CDQ分治:三维偏序
  • 原文地址:https://www.cnblogs.com/zkycode/p/6472204.html
Copyright © 2011-2022 走看看