Javascript sums total with addrow



首先,对不起,我的英语不好。

所以,我想对我的 2 个列求和,但我有 add.row 条件。这是我的代码:

<script>
$(function(){
  $('.qty, .unit').on('blur', function(e){
    var qty = parseFloat( $('.qty').val() ), 
        unit = parseFloat( $('.unit').val() );
    
    if( isNaN(qty) || isNaN(unit) ) {
      $('.result').text('');
      return false;
    }
    
    var total = qty * unit;
    $('.result').text( total );
  });
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td><input type="text" class="form-control qty" name="p_qty[]" /></td>
<td><input type="text" class="form-control unit" name="p_harga[]" /></td>
<td><strong><span class="result" name="p_jumlah[]"></span></strong></td>

这是我的屏幕截图屏幕截图

当 IM 运行时,此代码成功,但仅在第 1 行中有效,而不适用于第 2++ 行我该如何解决它?

非常感谢。

试试这个

<script src="../js/plugins/jquery.js"></script>
<script>
    $(function () {
        $('.form-control').blur(function () {
            var qty = $('.qty').val();
            var total = 0;
            $('.unit').each(function () {
                total += parseInt(this.value);
            });
            // Update the total
            $('.result').text(qty * total);
        });
    });
</script>
<input type="text" class="form-control qty" name="p_qty[]" />
<input type="text" class="form-control unit" name="p_harga[]" />
<!-- Two or more fields -->
<input type="text" class="form-control unit" name="p_harga[]" />
<input type="text" class="form-control unit" name="p_harga[]" />
<strong><span class="result" name="p_jumlah[]"></span></strong>

最新更新