【问题标题】:Calculate sum of dynamically adding input fields with jquery用jquery计算动态添加输入字段的总和
【发布时间】:2017-03-31 07:25:03
【问题描述】:

我有一个表格,用于计算金额的总和。使用 jquery clone() 动态生成行;

此代码正在计算渲染行的总和,而不是新创建的行

 $(document).ready(function(){

        $('.datepicker').datepicker();

        var $inst = $('.inst_amount');
            $inst_form = $('.inst_form');
            $total_amount = $('#total_amount');
            $total_price = $('#total_price');
            total = 0;

        $.each($inst, function(index, val) {
            var val = ($(this).val() == "") ? 0 : $(this).val();
            total = total + parseFloat(val);
        });
         $total_price.html(Math.round(total));
         $total_amount.val(Math.round(total));


        $(document).on('blur','.inst_amount', function(){
            var total = 0;
            $.each($inst, function(index, val) {
                var val = ($(this).val() == "") ? 0 : $(this).val();
                total = total + parseFloat(val);
            });
            console.log(total);
            $total_price.html(Math.round(total));
            $total_amount.val(Math.round(total));
        }); 
});

【问题讨论】:

  • var $inst = $('.inst_amount');移动到你使用它的地方:var total = 0,$inst = $('.inst_amount');
  • 这里有问题吗?

标签: php jquery laravel


【解决方案1】:
  1. var $inst = $('.inst_amount'); 移动到您使用它的位置:
    var total = 0,$inst = $('.inst_amount'); - 实际上甚至不要。查看我的代码

  2. 使用逗号分隔变量

  3. 干燥 - 不要重复自己

  4. 正确给出类并将字段添加到容器中。

function sumIt() {
  var total = 0, val;
  $('.inst_amount').each(function() {
    val = $(this).val();
    val = isNaN(val) || $.trim(val) === "" ? 0 : parseFloat(val);
    total += val;
  });
  $('#total_price').html(Math.round(total));
  $('#total_amount').val(Math.round(total));
}

$(function() {

  // $('.datepicker').datepicker(); // not needed for this test


  $("#add").on("click", function() {
    $("#container input").last()
      .before($("<input />").prop("class","inst_amount").val(0))
      .before("<br/>");
    sumIt();  
  });


  $(document).on('input', '.inst_amount', sumIt);
  sumIt() // run when loading
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="add" value="add" /><br/>
<form id="inst_form">
<div id="container">
  <input type="text" class="inst_amount" value="4" /><br/>
  <input type="text" class="inst_amount" value="" /><br/>
  <input type="text" id="total_amount" value="0" />
</form>
</div>
<span id="total_price"></span>

【讨论】:

  • 不,它正在计算渲染值而不是动态添加的输入值。
  • 查看更新 - 我留下了一个字段 value="" 来测试它仍然有效
【解决方案2】:

此代码将仅计算页面加载时可用的所有行的总数。添加新行时,您需要调用相同的代码再次计算总计。基本上点击添加行需要重新计算。

【讨论】:

    【解决方案3】:

    将这些代码放在 $(document).ready 之外

    $(document).on('blur','.inst_amount', function(){
            var total = 0;
            $.each($inst, function(index, val) {
                var val = ($(this).val() == "") ? 0 : $(this).val();
                total = total + parseFloat(val);
            });
            console.log(total);
            $total_price.html(Math.round(total));
            $total_amount.val(Math.round(total));
        });
    

    因为在 $(document).ready 时,不会添加新行

    【讨论】:

    • 你错了。每次触发模糊时,他都需要获取 $inst。查看我的代码
    • @Meera Tank,我已经尝试过您的解决方案,但它不起作用
    • @RizwanSaleem 请您尝试使用 $.each('.inst_amount', function(index, val) 而不是 $.each($inst, function(index, val) 因为您不会将 $inst 从 $(document).ready 中取出
    猜你喜欢
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-26
    • 1970-01-01
    • 2021-05-21
    • 2013-10-22
    • 2019-11-10
    相关资源
    最近更新 更多