Iam working on an application with certain calculations. I need to show and sum up the totals of Total INR and Margin Field. When a user put a percentage in Add (%) it calculates certain things and show the margin. And at the end script has to show the sum totals. Its showing Nan as of now.

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

My script is below:

我的脚本如下:

<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 个解决方案

#1


0

Try to use parseInt() to work really with integers instead of strings. If you read values from some html elements, you get these values as string. It also can cause wrong calculations. Just call parseInt() each time you read numeric value from html page element

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

更多相关文章

  1. 如何判断字符串是一个字符串化的JSON对象
  2. JavaScript相当于Ruby的字符串#扫描
  3. 11、javascript中字符串常用操作总结、JS字符串操作大全
  4. 从占用转义字符的字符数组创建字符串
  5. 使用没有后端脚本的Angularjs上传文件(例如PHP,JAVA等)
  6. javascript如何取字符串中的数字
  7. JavaScript中,提取子字符串方法:Slice、Substring、Substr的比较
  8. java 如何获取动态网页内容,返回字符串
  9. NodeJS - 解析JSON(只有字符串或数字)

随机推荐

  1. javascript中的属性类型
  2. [javascript 实践篇]——那些你不知道的
  3. 如何测试从实时网站提取数据的AJAX应用程
  4. Objective-C方法/函数调用。 (来自javascr
  5. Javascript函数的4种调用方法详解
  6. React.js中的setState vs replaceState
  7. 选择无线电输入时,无法提交带有JavaScript
  8. 【JavaScript】中两个小括号 ()() 是什么
  9. jQuery的几大难点
  10. 利用javascript实现遍历xml文件的代码实