如何使用jquery计算多个输入的发票总金额?

发布于 2025-01-10 21:50:13 字数 3604 浏览 3 评论 0原文

我使用 foreach 循环从 order_items 表中检索所有商品,然后我想修改每个商品的数量和价格。

我想要的是使用 jQuery 立即计算所有项目的 subtotaltotal 数量,但我找不到让它工作的方法。

$(".price").on('change', function() {
  var subtotal = 0;
  var total = 0;
  var total_ht = $("#total_ht").val();
  var price = $(this).val();
  var id = $(this).attr('data-id');
  var quantity = $("#quantity-" + id).val();

  subtotal = price * quantity;
  total = subtotal + (subtotal * 20 / 100)
  // Single row subtotal
  $("#subtotal-" + id).val(subtotal);
  // Single row total
  $("#total-" + id).val(total);
  // All  rows subtotal
  total_ht = parseFloat(total_ht + subtotal);
  $("#total_ht").val(total_ht);
})


$(".quantity").on('change', function() {
  var subtotal = 0;
  var total = 0;
  var total_ht = $("#total_ht").val();
  var quantity = $(this).val();
  var id = $(this).attr('data-id');
  var price = $("#price-" + id).val();

  subtotal = price * quantity;
  total = subtotal + (subtotal * 20 / 100);
  // Single row subtotal   
  $("#subtotal-" + id).val(subtotal);
  // Single row total
  $("#total-" + id).val(total);
  // All  rows subtotal
  $("#total_ht").val(total_ht);
})
<table>
      <tbody>
        <?php foreach($orderItems as $item): ?>
          <tr>
            <td>
              <input type="number" name="quantity[]" class="quantity" value="<?= $item->quantity ?>" data-id="<?= $item->id ?>" id="quantity-<?= $item->id ?>">
            </td>
            <td>
              <input type="number" name="price[]" class="form-control price" data-id="<?= $item->id ?>" value="<?= $item->unity_price ?>" id="price-<?= $item->id ?>">
            </td>

            <td>
              <input type="number" name="subtotal[]" class="subtotal" data-id="<?= $item->id ?>" value="<?= $item->total_ht_price ?>" id="subtotal-<?= $item->id ?>" readonly>
            </td>

            <td>
              <input type="number" name="total[]" class="total" data-id="<?= $item->id ?>" value="<?= $item->total_ttc_price ?>" id="total-<?= $item->id ?>" readonly>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <table class="table table-white">
    <tbody>
        <tr>
            <th class="text-left">Total HT
            </th>
            <td class="text-right">
                <input type="number" name="total_ht" id="total_ht" class="form-control total_ht" readonly value="0">
            </td>
        </tr>
        <tr>
            <th class="text-left">TVA
            </th>
            <td class="text-right">
                <input type="number" name="totalTVA" id="totalTVA" class="form-control totalTVA" readonly value="20">
            </td>
        </tr>
        <tr>
            <th class="text-left">Total TTC
            </th>
            <td class="text-right">
                <input type="number" name="totalTTC" id="totalTTC" class="totalTTC" readonly value="0">
            </td>
        </tr>
        
    </tbody>
    </table>


输入图片此处描述

I retrieve all items from order_items table using a foreach loop and then I want to modify quantity and prices of each item.

What I want is to calculate instantly the subtotal and total amount of all items using jQuery but I couldn't find a way to make it work.

$(".price").on('change', function() {
  var subtotal = 0;
  var total = 0;
  var total_ht = $("#total_ht").val();
  var price = $(this).val();
  var id = $(this).attr('data-id');
  var quantity = $("#quantity-" + id).val();

  subtotal = price * quantity;
  total = subtotal + (subtotal * 20 / 100)
  // Single row subtotal
  $("#subtotal-" + id).val(subtotal);
  // Single row total
  $("#total-" + id).val(total);
  // All  rows subtotal
  total_ht = parseFloat(total_ht + subtotal);
  $("#total_ht").val(total_ht);
})


$(".quantity").on('change', function() {
  var subtotal = 0;
  var total = 0;
  var total_ht = $("#total_ht").val();
  var quantity = $(this).val();
  var id = $(this).attr('data-id');
  var price = $("#price-" + id).val();

  subtotal = price * quantity;
  total = subtotal + (subtotal * 20 / 100);
  // Single row subtotal   
  $("#subtotal-" + id).val(subtotal);
  // Single row total
  $("#total-" + id).val(total);
  // All  rows subtotal
  $("#total_ht").val(total_ht);
})
<table>
      <tbody>
        <?php foreach($orderItems as $item): ?>
          <tr>
            <td>
              <input type="number" name="quantity[]" class="quantity" value="<?= $item->quantity ?>" data-id="<?= $item->id ?>" id="quantity-<?= $item->id ?>">
            </td>
            <td>
              <input type="number" name="price[]" class="form-control price" data-id="<?= $item->id ?>" value="<?= $item->unity_price ?>" id="price-<?= $item->id ?>">
            </td>

            <td>
              <input type="number" name="subtotal[]" class="subtotal" data-id="<?= $item->id ?>" value="<?= $item->total_ht_price ?>" id="subtotal-<?= $item->id ?>" readonly>
            </td>

            <td>
              <input type="number" name="total[]" class="total" data-id="<?= $item->id ?>" value="<?= $item->total_ttc_price ?>" id="total-<?= $item->id ?>" readonly>
            </td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

    <table class="table table-white">
    <tbody>
        <tr>
            <th class="text-left">Total HT
            </th>
            <td class="text-right">
                <input type="number" name="total_ht" id="total_ht" class="form-control total_ht" readonly value="0">
            </td>
        </tr>
        <tr>
            <th class="text-left">TVA
            </th>
            <td class="text-right">
                <input type="number" name="totalTVA" id="totalTVA" class="form-control totalTVA" readonly value="20">
            </td>
        </tr>
        <tr>
            <th class="text-left">Total TTC
            </th>
            <td class="text-right">
                <input type="number" name="totalTTC" id="totalTTC" class="totalTTC" readonly value="0">
            </td>
        </tr>
        
    </tbody>
    </table>


enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独留℉清风醉 2025-01-17 21:50:13

这是我使用输入事件的委托版本

,因为它也会在粘贴时触发

$("table tbody tr input").on('input', function() {
  let total = 0;
  $("table tbody tr").each(function() {
    const price = +$(this).find(".price").val()
    const qty = +$(this).find(".quantity").val()
    const val = price*qty
    total += val;
    $(this).find(".subtotal").val(val)
    $(this).find(".total").val(total)
    
  })
  $("#total_ht").val(total)
}).trigger("input")
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>

      <tr>
        <td>
          <input type="number" name="quantity[]" class="quantity" value="2" data-id="id1" id="quantity-1">
        </td>
        <td>
          <input type="number" name="price[]" class="form-control price" data-id="id2" value="3" id="price-1">
        </td>

        <td>
          <input type="number" name="subtotal[]" class="subtotal" data-id="id3" value="" id="subtotal-1" readonly>
        </td>

        <td>
          <input type="number" name="total[]" class="total" data-id="id4" value="" id="total-1" readonly>
        </td>
      </tr>

      <tr>
        <td>
          <input type="number" name="quantity[]" class="quantity" value="2" data-id="id5" id="quantity-2">
        </td>
        <td>
          <input type="number" name="price[]" class="form-control price" data-id="id6>" value="3" id="price-2">
        </td>

        <td>
          <input type="number" name="subtotal[]" class="subtotal" data-id="id7" value="" id="subtotal-2" readonly>
        </td>

        <td>
          <input type="number" name="total[]" class="total" data-id="id9" value="" id="total-2" readonly>
        </td>
      </tr>

  </tbody>
</table>
<input type="number" id="total_ht" readonly />

Here is a delegated version

I use input event since it triggers on paste too

$("table tbody tr input").on('input', function() {
  let total = 0;
  $("table tbody tr").each(function() {
    const price = +$(this).find(".price").val()
    const qty = +$(this).find(".quantity").val()
    const val = price*qty
    total += val;
    $(this).find(".subtotal").val(val)
    $(this).find(".total").val(total)
    
  })
  $("#total_ht").val(total)
}).trigger("input")
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody>

      <tr>
        <td>
          <input type="number" name="quantity[]" class="quantity" value="2" data-id="id1" id="quantity-1">
        </td>
        <td>
          <input type="number" name="price[]" class="form-control price" data-id="id2" value="3" id="price-1">
        </td>

        <td>
          <input type="number" name="subtotal[]" class="subtotal" data-id="id3" value="" id="subtotal-1" readonly>
        </td>

        <td>
          <input type="number" name="total[]" class="total" data-id="id4" value="" id="total-1" readonly>
        </td>
      </tr>

      <tr>
        <td>
          <input type="number" name="quantity[]" class="quantity" value="2" data-id="id5" id="quantity-2">
        </td>
        <td>
          <input type="number" name="price[]" class="form-control price" data-id="id6>" value="3" id="price-2">
        </td>

        <td>
          <input type="number" name="subtotal[]" class="subtotal" data-id="id7" value="" id="subtotal-2" readonly>
        </td>

        <td>
          <input type="number" name="total[]" class="total" data-id="id9" value="" id="total-2" readonly>
        </td>
      </tr>

  </tbody>
</table>
<input type="number" id="total_ht" readonly />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文