没有得到Jquery / Javascript中的总和

时间:2015-11-24 08:01:36

标签: javascript jquery

我正在处理具有某些计算的应用程序。我需要显示和总结Total INR和Margin Field的总数。当用户在Add(%)中添加一个百分比时,它会计算某些内容并显示边距。最后,脚本必须显示总和。它现在显示为Nan。

我的脚本如下:

<html>
    <head>
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>  
        <script type='text/javascript'>//<![CDATA[
            function isNum(value)
            {
                return 123;
            }

            function calcTotals1()
            {
                var grandTotal = 0;
                var i = 0;

                while (document.forms['cart'].elements['add_percentage[]'][i])
                {
                    add_percentageObj = document.forms['cart'].elements['add_percentage[]'][i];
                    addon_valueObj   = document.forms['cart'].elements['addon_value[]'][i];
                    total_inr_valueObj   = document.forms['cart'].elements['total_inr[]'][i];

                    totalObj = document.forms['cart'].elements['add_value[]'][i];
                    marginObj = document.forms['cart'].elements['margin_for[]'][i];


                                if (isNaN(add_percentageObj.value)) {
                                    add_percentageObj = '';
                                }
                                if (isNaN(addon_valueObj.value)) {
                                    addon_valueObj = '';
                                }

                                if (add_percentageObj.value != 0) {
                                    totalObj.value = (((total_inr_valueObj.value * 1) * add_percentageObj.value / 100) + total_inr_valueObj.value * 1).toFixed(3);
                                    grandTotal = grandTotal + parseFloat(totalObj.value);
                                    marginObj.value = ((totalObj.value * 1) - (total_inr_valueObj.value * 1)).toFixed(3);
                                    margin_total = ((margin_total *1) + marginObj.value * 1);

                                } else if (addon_valueObj.value !=0) {
                                    totalObj.value = ((addon_valueObj.value * 1) + total_inr_valueObj.value * 1).toFixed(3);
                                    grandTotal = grandTotal + parseFloat(totalObj.value);
                                    marginObj.value = ((totalObj.value * 1) - (total_inr_valueObj.value * 1)).toFixed(3);
                                    margin_total = ((margin_total *1) + marginObj.value * 1);

                                } else {
                                    totalObj.value = ((addon_valueObj.value * 1) + total_inr_valueObj.value * 1).toFixed(3);
                                    grandTotal = grandTotal + parseFloat(totalObj.value);
                                    marginObj.value = ((totalObj.value * 1) - (total_inr_valueObj.value * 1)).toFixed(3);
                                    margin_total = ((margin_total *1) + marginObj.value * 1);

                                }

                                i++;
                            }

                            document.getElementById('grand_total').value = grandTotal.toFixed(3);
                            document.getElementById('margin_total').value = margin_total.toFixed(3);
                            //document.getElementById('total_inr1').value = total_inr1.toFixed(3);
                            //document.getElementById('margin_for').value = margin_for;


                            return;

                        }



        </script>
    </head>
    <body>
        <form name="cart" class="single">
            <div align="center" class="base">
                <table width="100%" border=1 style="border-collapse: collapse;">
                    <tr bgcolor="#E6E6FA">
                        <td colspan="6"><b>Terms and Cost</b></td>
                    </tr>
                    <tr>
                        <td>Terms</td>
                        <td>Total INR</td>
                        <td>Add (%)</td>
                        <td>Add Value</td>
                        <td>Total Value</td>
                        <td>Margin</td>
                    </tr>
                    <tr>
                        <td width="25%">Export Charges</td>
                        <td width="75%"><input type="text" id="total_inr[]" name="total_inr[]" value="13263.984"></td>
                        <td><input type="text" id="add_percentage[]" name="add_percentage[]" value="" onchange="calcTotals1()"></td>
                        <td><input type="text" id="addon_value[]" name="addon_value[]" value="" onchange="calcTotals1()"></td>
                        <td><input type="text" id="add_value[]" name="add_value[]" value=""></td>
                        <td><input type="text" id="margin_for[]" name="margin_for[]" value=""></td>
                    </tr>
                    <tr>
                        <td width="25%">IATA Charges</td>
                        <td width="75%"><input type="text" id="total_inr[]" name="total_inr[]" value="16579.98"></td>
                        <td><input type="text" id="add_percentage[]" name="add_percentage[]" value="" onchange="calcTotals1()"></td>
                        <td><input type="text" id="addon_value[]" name="addon_value[]" value="" onchange="calcTotals1()"></td>
                        <td><input type="text" id="add_value[]" name="add_value[]" value=""></td>
                        <td><input type="text" id="margin_for[]" name="margin_for[]" value=""></td>
                    </tr>
                    <tr>
                        <td></td>
                        <td><input name="gTotal" id="grand_total" style="font-weight: bold" size="20" /></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td><input type='text' style='font-weight: bold'  id='margin_total' name='margin_total' size='8' readonly /></td>
                    </tr>
                </table>
            </div>
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试使用parseInt()来实际使用整数而不是字符串。如果您从某些html元素中读取值,则会将这些值设为string。它也可能导致错误的计算。每次从html页面元素

中读取数值时,只需调用parseInt()