douzhi3779 2016-02-18 09:37
浏览 32
已采纳

循环中的Jquery执行一些记录计算

Iam trying to bring some records using php and do some calculations. What iam doing now is that, each rows is having a dropdown with different currencies. When i select each currency, it calculates and shows certain values. Till here its working fine.

What i am trying to achieve is that if i select first currency dropdown, it should calculate the complete records calculations instead of selecting the currency of each rows. I guess i need to do some kind of loop in the jquery which calculates the rows.

Fiddle

Following is the part of jquery script for the currency dropdown.

$(window).load(function() {
  $(document).ready(function() {
    $("select").on('change', function() {

      var dept_number = $(this).val();
      var price = $(this).find(':selected').data('price');
      var selected = $(this).find('option:selected').text();

      if (selected == "INR") {
        $(this).closest('table').find('.total1').val($(this).closest('table').find('.total').val());
      } else {
        $(this).closest('table').find('.total1').val((($(this).closest('table').find('.total').val() * price) / $(this).closest('table').find('.inrvalue').val()).toFixed(3));
      }

      $(this).closest('table').find('.price_unit').val(($(this).closest('table').find('.total1').val() / $(this).closest('table').find('.qty').val()).toFixed(3));

    });
  });
});

i guess i need to add some loops here in this jquery. Anyone to guide me how to do this. Or i need to follow a different step.

This is what i have tried as per the suggestion from Leonix.

$(document).ready(function(){
            $(this).closest('table').find("select").each(function() {
               var dept_number = $(this).val();
               var price = $(this).find(':selected').data('price');

                var selected = $(this).find("select");

           if(selected=="INR")
            {
               $(this).closest('table').find('.total1').val($(this).closest('table').find('.total').val());

            } else
            {

           $(this).closest('table').find('.total1').val((($(this).closest('table').find('.total').val() * price) / $(this).closest('table').find('.inrvalue').val()).toFixed(3));
           }

               $(this).closest('table').find('.price_unit').val(($(this).closest('table').find('.total1').val()/$(this).closest('table').find('.qty').val()).toFixed(3));

           });
           });
  • 写回答

1条回答 默认 最新

  • dougu2240 2016-02-18 10:08
    关注

    In your select change function, do a each for all rows of your table and find the dropdown:

    $(this).closest('table').find("select").each(function() {
        /* Each SELECT here, use $(this) */
    })
    

    or, depending of your needs :

    $(this).closest('table').find("select").each(function() {
        /* Each TR here, use selectInput */
        var selectInput = $(this).find("select");
    })
    

    With the select in hands, use selectInput.val(changedSelectInput.val()) changedSelectInput is the jquery object containing the select who changed.
    Using nested anonymous functions, take care, they are executed in the object context, so this and $(this) change depending on the object affected by function.

    Advice: Use specific css classes for JS, as select.select-currency instead of select only, put these classes in your code. it will prevent so many mistakes and will save your time.

    Note: currency_change[] is not a valid ID, if you dont need to set one, dont.

    EDIT

    Some code: https://jsfiddle.net/btpxq5ow/6/
    What I did ?

    1. Fix tags issues
    2. Fix input in tbody issues, NEVER put it in a tr, tbody, table directly.
    3. Fix some indentation issues
    4. Apply the currency change to all rows
    5. Prevent change event to call itself in an infinite loop
    6. Apply calculation to all rows when they are updated
    7. Fix some code syntax & performance issues

    Please check your calculation are right since i modified it, see calculateRowTotals().
    There are still a few html/js errorsthat must be fixed.

    You will rarely get code from stackoverflow

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分