亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 jQuery 在發票中實現折扣值

如何使用 jQuery 在發票中實現折扣值

PHP
ABOUTYOU 2023-10-21 10:09:12
我正在嘗試在我的表單中實現折扣值。我在 jQuery 代碼中嘗試過這個$('tbody').delegate('.quantity,.price,.gst,.dsc', 'keyup', function() {  var tr = $(this).parent().parent();  var quantity = tr.find('.quantity').val();  var price = tr.find('.price').val();  var gst = tr.find('.gst').val();  var dcs = tr.find('.dcs').val();  var totalamountgst = (quantity * price * gst) / 100;  var totalamountdcs = (quantity * price * dcs) / 100;  var totalamounts = (quantity * price + totalamountgst);  var totalamount = (totalamounts - totalamountdcs);  tr.find('.totalamount').val(totalamount);  total();});function total() {  var total = 0;  $('.totalamount').each(function(i, e) {    var totalamount = $(this).val() - 0;    total += totalamount;  });  $('.total').val(total + ".00");} <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table>  <tbody>    <tr>      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control quantity" name="qty[]" id="validationServer01" placeholder="QTY" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control gst " name="gst_amount[]" id="validationServer01" placeholder="GST" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control dcs " name="dcs_amount[]" id="validationServer01" placeholder="DCS" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>      <td> <input style="text-align:center;" type="number" class="text-danger  input-lg form-control price" name="purchase_rate[]" id="validationServer01" placeholder="RATE" value="" required pattern="" numbers="" autocomplete="off"></td>    </tr>  </tbody></table>
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

你有拼寫錯誤并且錯過了總數。


當 ppl 輸入非數字或將它們全部設置為 type=number 時,您確實還應該測試空和 isNaN


$('tbody').on('input', '.quantity,.price,.gst,.dsc', function() {

  const $tr = $(this).closest("tr"); // parent row

  const quantity = $tr.find('.quantity').val() || 0; // value of input fields

  const price = $tr.find('.price').val() || 0;

  const gst = $tr.find('.gst').val() || 0;

  

  const $dcsField = $tr.find('.dcs'); // the field is cached so we can reuse

  const dcs = $dcsField.val(); // here is the value


  const totalamountgst = (quantity * price * gst) / 100;

  const totalamountdcs = (quantity * price * dcs) / 100;

  const totalamounts = (quantity * price + totalamountgst);

  const totalamount = (totalamounts - totalamountdcs);

  

  $dcsField.data("dcsamt",totalamountdcs || 0); // save the discounted value in a data attribute to be summed


  $tr.find('.totalamount').val(totalamount.toFixed(2));

  total();

});


function total() {

  let total = 0,

    totalQty = 0,

    totalDcs = 0;

  $('.totalamount').each(function(i, e) {

    const totalamount = $(this).val() - 0;

    total += totalamount;

    const q = $(this).closest("tr").find(".quantity").val() || 0;

    const d = $(this).closest("tr").find(".dcs").data("dcsamt") || 0;

    totalQty += q - 0;

    totalDcs += d - 0; // take the actual discount from the attribute

  });

  $('#total').val(total.toFixed(2));

  $('#totalitems').val(totalQty.toFixed(2));

  $('#totaldcs').val(totalDcs.toFixed(2));

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>

  <tbody>

    <tr>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control quantity" name="qty[]" id="validationServer01" placeholder="QTY" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control gst " name="gst_amount[]" id="validationServer01" placeholder="GST" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control dcs " name="dcs_amount[]" id="validationServer01" placeholder="DCS" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="number" class="text-danger  input-lg form-control price" name="purchase_rate[]" id="validationServer01" placeholder="RATE" value="" required pattern="" numbers="" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control totalamount" name="" id="validationServer01" placeholder="AMOUNT" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" disabled></td>

    </tr>

    <tr>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control quantity" name="qty[]" id="validationServer01" placeholder="QTY" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control gst " name="gst_amount[]" id="validationServer01" placeholder="GST" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control dcs " name="dcs_amount[]" id="validationServer01" placeholder="DCS" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="number" class="text-danger  input-lg form-control price" name="purchase_rate[]" id="validationServer01" placeholder="RATE" value="" required pattern="" numbers="" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control totalamount" name="" id="validationServer01" placeholder="AMOUNT" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" disabled></td>

    </tr>

    <tr>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control quantity" name="qty[]" id="validationServer01" placeholder="QTY" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control gst " name="gst_amount[]" id="validationServer01" placeholder="GST" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control dcs " name="dcs_amount[]" id="validationServer01" placeholder="DCS" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="number" class="text-danger  input-lg form-control price" name="purchase_rate[]" id="validationServer01" placeholder="RATE" value="" required pattern="" numbers="" autocomplete="off"></td>

      <td> <input style="text-align:center;" type="text" class="text-danger  input-lg form-control totalamount" name="" id="validationServer01" placeholder="AMOUNT" value="" required pattern="[1-2-3-4-3-5-6-7-8-9-10-12]+" numbers="onlynumbers" disabled></td>

    </tr>

    <tr>

      <td> <input type="text" class="text-danger input-lg form-control" id="totalitems" name="" placeholder="TOTAL ITEMS" value=""></td>

      <td></td>

      <td> <input type="text" class="input-lg form-control" id="totaldcs" name="" placeholder="TOTAL DCS" value=""></td>

      <td></td>

      <td><input type="text" readonly id="total"></td>

    </tr>

  </tbody>

</table>


查看完整回答
反對 回復 2023-10-21
  • 1 回答
  • 0 關注
  • 175 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號