如何自动将动态文本字段的sub_total值的总和转换为总文本字段

时间:2019-05-06 15:58:18

标签: javascript php

我想获取总文本字段中sub_total文本字段的值的总和可以根据用户要求添加行...但是..我尝试了大多数事情..但没有弄清楚如何获取总和没有任何用户输入的情况下,total文本字段中所有sub_total文本框值的总和,有人可以用此代码来帮助我.....

先谢谢了。

i = 1;
$('.newRow').on('click', '.addRow', function(e) {
      $('.nR').append('<tr>' +
        '<td class="custom-td">' +
        '<select id="sp_' + i + '"  class="s_p select_product form-control input-sm" name="product[]" style="width:25em;" onchange="get_Result(' + i + ')">' +
        '<option></option>' +
        '@foreach ($products as $key=>$product_name)' +
        '<option value="{{$key}}">{{$product_name}}</option>' +
        '@endforeach' +
        '</select>' +
        '</td>' +
        '<td class="custom-td">' +
        '<input type="text" min="0" id="qty_' + i + '" class="qty form-control input-sm" name="quantity[]" autocomplete="off" value="1" onKeyUp="get_Result(' + i + ')">' +
        '</td>' +
        '<td class="custom-td">' +
        '<input type="text" id="up_' + i + '" class="up unit-price form-control input-sm" name="unit_price[]" readonly>' +
        '</td>' +
        '<td class="custom-td">' +
        '<input type="text" min="0" id="dis_' + i + '" class="form-control input-sm" name="discount[]" autocomplete="off" value="0" onKeyUp="get_Result(' + i + ')">' +
        '</td>' +
        '<td class="custom-td">' +
        '<input type="text" id="sub-total_' + i + '" class="s-t st form-control input-sm" name="sub_total[]" readonly>' +
        '</td>' +
        '<td style="text-align:center;">' +
        '<i data-toggle="tooltip" data-placement="top" title="delete" style="cursor:pointer;" class="tt red far fa-times-octagon remove"></i>' +
        '</td>' +
        '</tr>');
      i++;

      var total = 0
      $('.s-t').each(function() {
        var $this = $(this);
        total += $this.val();
      });
      $("#total").val(total);
<table class="item_table newRow table table-bordered table-sm mt-3" style="width:100%;">
  <tr style="font-size:12px;">
    <th>Product</th>
    <th>Quantity</th>
    <th>Unit Price (PKR)</th>
    <th>Discount</th>
    <th>Sub-Total</th>
    <th>Option</th>
  </tr>
  <tbody class="nR">
    <tr>
      <td class="custom-td">
        <select class="select_product s_p form-control input-sm" name="product[]" style="width:25em;">
          <option></option>
          @foreach ($products as $key=>$product_name)
          <option value="{{$key}}">{{$product_name}}</option>
          @endforeach
        </select>
      </td>
      <td class="custom-td">
        <input type="text" min="0" id="qty_0" class="qty form-control input-sm" name="quantity[]" autocomplete="off" value="1" onKeyUp="get_Result(0)">
      </td>
      <td class="custom-td">
        <input type="text" id="up_0" class="up unit-price form-control input-sm" name="unit_price[]" readonly>
      </td>
      <td class="custom-td">
        <input type="text" min="0" id="dis_0" class="form-control input-sm" name="discount[]" autocomplete="off" value="0" onkeyUp="get_Result(0)">
      </td>
      <td class="custom-td">
        <input type="text" id="sub-total_0" class="s-t st form-control input-sm" name="sub_total[]" readonly>
      </td>
      <td style="text-align:center;">
        <i data-toggle="tooltip" data-placement="top" title="delete" style="cursor:pointer;" class="red far fa-times-octagon remove"></i>
      </td>
    </tr>
  </tbody>
  <tr>
    <td colspan="6" style="padding-left:5px;">
      <button type="button" class="btn btn-default btn-sm addRow"><i class="fal fa-plus"></i> Add</button>
    </td>
  </tr>
</table>

<input type="text" id="total" style="border:none;background:#f7f7f7;text-align:right;font-weight:500;" class="form-control input-sm" name="total" readonly>

0 个答案:

没有答案