【问题标题】:How to Sum up last 12 month amounts in javascript dynamically如何在javascript中动态总结过去12个月的金额
【发布时间】:2014-02-14 22:38:36
【问题描述】:

我需要在每个下个月编写一个关于过去 12 个月计算的代码,以便当用户输入当月或下个月的值时,他应该得到结果 这里是如何计算的

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

如果输入下个月的值,我每个月都需要动态显示结果......我创建的是一个月......请帮我解决问题

【问题讨论】:

    标签: javascript


    【解决方案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);
    });
    

    【讨论】:

    • 感谢您的回复,感谢您将 JavaScript 转换为 JQuery,现在我几乎很平静,评论很好,我正在改变这些东西......如果我想取最后 6 个的平均值App.CA.. 的值可以包含在同一个函数中,还是我必须编写一个单独的函数,否则我可以包含在同一个函数中...
    猜你喜欢
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 2010-10-11
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2019-07-10
    相关资源
    最近更新 更多