简体   繁体   中英

Set another element value in each function jquery

I want to set another element value in element each function. my script (JS):

function getTotal() { $(':input[id^="barang_qty[]"]').keyup(function() {
var total = 0;
var $inputs = $(':input[id^="barang_qty[]"]');
$inputs.each(function (index) { total += parseFloat($(this).val()) *  parseFloat($(this).attr('data-price'));
         $("#barang_total[]").val(total); 
     });  
$('#total').val(total); 
}); 

HTML :

<td class="price">
                    <input type="text" name="barang_unit_price[]" readonly id="barang_unit_price[]" class="form-control">
                    </td>
                    <td>
                    <input type="text" name="barang_qty[]" id="barang_qty[]" onkeyup="getTotal()" class="form-control" >
                    </td>
                    <td>
                    <input type="text" name="barang_total[]" class="sum form-control" >
                    </td>

but element barang_total still empty.

Regards, Thank You

The problem is you are adding a keyup handler in the getTotal method, so the first key up does not do any calculation.

Instead either you can do the calculation in the getTotal() method, with a inline call

function getTotal() {
  var total = 0;
  var $inputs = $('input[name="barang_qty[]"]');
  $inputs.each(function(index) {
    var sum = parseFloat($(this).val()) * parseFloat($(this).attr('data-price')) || 0;
    total += sum;
    $(this).closest('tr').find('[name="barang_total[]"]').val(sum);
  });
  $('#total').val(total);
}

Or use dom ready handler to register the event handler instead of using inline event handlers.

<input type="text" name="barang_qty[]" id="barang_qty[]" class="form-control" >

then

jQuery(function() {
  $('input[name="barang_qty[]"]').keyup(function() {
    var total = 0;
    var $inputs = $('input[name="barang_qty[]"]');
    $inputs.each(function(index) {
      var sum = parseFloat($(this).val()) * parseFloat($(this).attr('data-price')) || 0;
      total += sum;
      $(this).closest('tr').find('[name="barang_total[]"]').val(sum);
    });
    $('#total').val(total);
  });
})

Note: If you are dealing with dynamic elements then you will have to use event delegation

Demo: Fiddle

try this

    function getTotal() { $(':input[id^="barang_qty[]"]').keyup(function() {
      var total = 0;
      var $inputs = $(':input[id^="barang_qty[]"]');
      $inputs.each(function (index) { total += parseFloat($(this).val()) * parseFloat($(this).attr('data-price'));
        (function (total) {
            $("#barang_total[]").val(total); 
        })(total);
     });  
     $('#total').val(total); 
}); 

You're missed the id for input has name barang_total[] .

Try this, (i've tested in jsbin, it's worked)

<td class="price">
  <input type="text" name="barang_unit_price[]" readonly id="barang_unit_price[]" class="form-control" value="100">
  </td>
  <td>
    <input type="text" name="barang_qty[]" data-price="100" id="barang_qty[]" onkeyup="getTotal()" class="form-control" value="2">
  </td>
  <td>
    <input type="text" name="barang_total[]" id="barang_total[]" class="sum form-control" >
  </td>

JS

function getTotal() { 
  $(':input[id^="barang_qty[]"]').keyup(function() {
  var total = 0;
  var $inputs = $(':input[id^="barang_qty[]"]');
  $inputs.each(function (index) {
    total += parseFloat($(this).val()) *  parseFloat($(this).attr('data-price'));
    $(':input[id^="barang_total[]"]').val(total); //edited this line
  });  
  $('#total').val(total); 
  }); 
}

Demo: http://jsbin.com/kijifisinu/edit?html,js,output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM