簡體   English   中英

在javascript中得到錯誤的計算

[英]Getting wrong calculation in javascript

我有下面的表..但在javascript中得到錯誤的計算..

在此輸入圖像描述

     <script>
         function getPrice(tRate, tMaking, tHandeling, tPrice, tVat, tQuantity, tTotal) {
             var obj_tRate = document.getElementById(tRate)
             var obj_tMaking = document.getElementById(tMaking)
             var obj_tHandeling = document.getElementById(tHandeling)
             var obj_tPrice = document.getElementById(tPrice)
             var obj_tVat = document.getElementById(tVat)
             var obj_tTotal = document.getElementById(tTotal)
             var obj_tQuantity = document.getElementById(tQuantity)
             if (obj_tRate.value != "" && obj_tMaking.value != "" && obj_tHandeling.value != "") {
                 obj_tPrice.value = parseFloat(obj_tRate.value) + parseFloat(obj_tMaking.value) + parseFloat(obj_tHandeling.value);
                 console.log(obj_tPrice.value)
                 obj_tVat.value = parseFloat(obj_tPrice.value * (1 / 100));
                 console.log(obj_tVat.value)
                 obj_tTotal.value = parseFloat(obj_tVat.value + (obj_tPrice.value * obj_tQuantity.value));
                 console.log(obj_tTotal.value)
             }
             else {
                 obj_tPrice.value = "";
             }
         }

</script>

</head>
<body>
    <table>
        <tr>
            <td>
                        <input name="grdView$ctl08$txtWaight_F" type="text" id="grdView_ctl08_txtWaight_F" class="classWaight" style="width:60px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtQuantity_F" type="text" maxlength="20" id="grdView_ctl08_txtQuantity_F" class="classQuantity" onchange="javascript:return getPrice('grdView_ctl08_txtRate_F','grdView_ctl08_txtMaking_F','grdView_ctl08_txtHandeling_F','grdView_ctl08_txtPrice_F','grdView_ctl08_txtvat_F','grdView_ctl08_txtQuantity_F','grdView_ctl08_txtTotal_F');" style="width:60px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtRate_F" type="text" maxlength="8" id="grdView_ctl08_txtRate_F" class="classRate" onchange="javascript:return getPrice('grdView_ctl08_txtRate_F','grdView_ctl08_txtMaking_F','grdView_ctl08_txtHandeling_F','grdView_ctl08_txtPrice_F','grdView_ctl08_txtvat_F','grdView_ctl08_txtQuantity_F','grdView_ctl08_txtTotal_F');" style="width:60px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtMaking_F" type="text" id="grdView_ctl08_txtMaking_F" class="classMaking" onchange="javascript:return getPrice('grdView_ctl08_txtRate_F','grdView_ctl08_txtMaking_F','grdView_ctl08_txtHandeling_F','grdView_ctl08_txtPrice_F','grdView_ctl08_txtvat_F','grdView_ctl08_txtQuantity_F','grdView_ctl08_txtTotal_F');" style="width:60px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtHandeling_F" type="text" id="grdView_ctl08_txtHandeling_F" class="classHandling" onchange="javascript:return getPrice('grdView_ctl08_txtRate_F','grdView_ctl08_txtMaking_F','grdView_ctl08_txtHandeling_F','grdView_ctl08_txtPrice_F','grdView_ctl08_txtvat_F','grdView_ctl08_txtQuantity_F','grdView_ctl08_txtTotal_F');" style="width:60px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtPrice_F" type="text" id="grdView_ctl08_txtPrice_F" class="classPrice" style="width:60px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtvat_F" type="text" id="grdView_ctl08_txtvat_F" class="classVat" style="width:60px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtTotal_F" type="text" id="grdView_ctl08_txtTotal_F" class="classTotal" style="width:100px;" />
                    </td><td>
                        <input name="grdView$ctl08$txtSerial_F" type="text" id="grdView_ctl08_txtSerial_F" class="classSerial" />
                    </td>
        </tr>
    </table>
</body>

在操作數上使用parseFloat ,而不是在計算結果上使用。

例如,替換

obj_tTotal.value = parseFloat(obj_tVat.value + (obj_tPrice.value * obj_tQuantity.value));

var tPrice = parseFloat(obj_tPrice.value);
var tQuantity = parseFloat(obj_tQuantity.value);
obj_tTotal.value = tPrice + tPrice * tQuantity;

當你像你一樣添加一個字符串和一個數字時,你會進行字符串連接。

例如

"1000" + "5" * "100"

"1000" + 500

是的

"1000500"

此時,調用parseFloat為時已晚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM