2014-02-14 55 views
0

我需要编写一个有关过去12个月的计算每月下一个月的代码,以便当用户输入当前月份或下个月的值时,他应该得到结果 这里计算如何如何总结过去12个月的数额在JavaScript动态

CA (jan 14) = (Avg of B.Y. 2001=100 for the past 12 months - 115.76)*100/115.76 
CA (jan 14)=(232.66-115.76)*100/115.76=100.009 

所以每个月如果有一些机构进入它,我们应该通过上述计算得到的值。 我在JavaScript中某种编码的尝试,请签在这个环节

http://jsfiddle.net/kundansingh/SLC5F/

B.Y. 2001年= 100 =值将是用户 的12个月 =的过去11个月累计值与当月所以12个月 %增加总超过115.763 = 232.66-115.76 应用进入。 CA =(232.66-115.76)* 100/115.76

我需要动态的每个月如果输入下个月的价值也应该显示结果...我创造了一个月..请帮助我对问题进行排序

回答

1

在这种情况下,我真的推荐使用jQuery让你的生活变得更容易,尤其是因为你至少要做一个for循环,如果不是两个。

您需要遍历每一行,获取前面的11个值,然后进行计算。

在你的jsFiddle中,下面的代码应该可以工作。我已经相应修改您的jsfiddle:http://jsfiddle.net/SLC5F/1/

$('tr', '#order-table').each(function() { 
    var total = 0; 
    var $rows = $(this).prevAll(); 

    // return if we don't have enough rows 
    // probably better to just add a class to the active rows instead, 
    // but it's not my job to format your code. 
    if ($rows.length < 13) return; 
    $rows = $rows.slice(0, 11); 

    // again, would be better to add a class to active inputs instead of using 
    // a not() filter. you can do this yourself. 
    total += parseFloat($(this).find('input').not('.row-total-input, [readonly]').val()); 

    $rows.each(function() { 
    total += parseFloat($(this).find('input').not('.row-total-input').val()); 
    }); 

    // return if total isn't a number 
    if (isNaN(total)) return; 

    var monthly_average = total/12; 

    // again, would be better to add classes to these TDs instead of using eq() 
    // make sure you do this before using this script in a live application. 
    $('td', this).eq(2).text(total); 
    $('td', this).eq(3).text(Math.round(monthly_average * 100)/100); 
    $('td', this).eq(4).text(Math.round((monthly_average - 115.763) * 100/115.764 * 100)/100); 
}); 
+0

谢谢你的答复,并转换成JavaScript的JQuery的,现在我几乎是在和平的评论是好的,我改变这些东西,如果我想采取App.CA的最后6个值的平均值..我可以包括在相同的功能,或者我必须写一个单独的或在相同的我可以包括... –