关于jQuery的更改不适用于可读取输入字段

发布于 2025-02-12 22:54:53 字数 1453 浏览 3 评论 0原文

我的形式有一些字段,如下所示:

$(document).on("change", ".qty2", function() {
  var sum1 = 0;
  $(".qty2").each(function() {
    sum1 += +$(this).val();
  });
  $("#subtotal").val(sum1);
});

$(document).on("change", "#subtotal", function() {

  var subt = $("#subtotal").val();

  var tot_price = (subt * 9 / 100);
  var divobj = document.getElementById('cgst');
  var divobj1 = document.getElementById('sgst');
  divobj.value = tot_price;
  divobj1.value = tot_price;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control qty1" id="subtotal" required name="subtotal" readonly>
<input type="text" class="form-control qty1" id="cgst" required name="cgst" readonly>
<input type="text" class="form-control qty1" id="sgst" required name="sgst" readonly>

除了第一个输入字段的“价格”之外,所有其他3个都被阅读,当用户进入价格字段时,该值被带到小计,我需要从那里计算SGST和CGST并将其显示在各自的字段中,但是,在我的代码中只有在小计显示器正常工作之前,任何人都可以告诉我如何解决此问题,谢谢

i have some fields in my form, which are like below:

$(document).on("change", ".qty2", function() {
  var sum1 = 0;
  $(".qty2").each(function() {
    sum1 += +$(this).val();
  });
  $("#subtotal").val(sum1);
});

$(document).on("change", "#subtotal", function() {

  var subt = $("#subtotal").val();

  var tot_price = (subt * 9 / 100);
  var divobj = document.getElementById('cgst');
  var divobj1 = document.getElementById('sgst');
  divobj.value = tot_price;
  divobj1.value = tot_price;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control qty1" id="subtotal" required name="subtotal" readonly>
<input type="text" class="form-control qty1" id="cgst" required name="cgst" readonly>
<input type="text" class="form-control qty1" id="sgst" required name="sgst" readonly>

here apart from the first input field 'price', all other 3 are readonly, when user enters something in price field, that value is taken to subtotal, and from there i need to calculate sgst and cgst and display it in their respective fields, however in my code only till subtotal display is working, can anyone please tell me how to fix this, thanks in advance

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

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

发布评论

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

评论(2

橘寄 2025-02-19 22:54:53

这是使用html的脚本重写

,如果您有一组字段,请将每个集合包装在DIV中并使用.closest(“ Div”)。查找(“。其他Field”)使此工作在例如购物清单中

const $qty = $(".qty2").on("input", () => { // cache the fields
  let total = $qty
    .map((i, fld) => +fld.value)
    .get()
    .reduce((a, b) => a + b);
  let tot_price = (total * 9 / 100).toFixed(2);
  $("#subtotal").val(total);
  $('#cgst').val(tot_price);
  $('#sgst').val(tot_price);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control price qty2" required name="price[]">
<input type="text" class="form-control price qty2" required name="price[]">
<input type="text" class="form-control price qty2" required name="price[]">
<input type="text" class="form-control qty1" id="subtotal" name="subtotal" readonly>
<input type="text" class="form-control qty1" id="cgst" name="cgst" readonly>
<input type="text" class="form-control qty1" id="sgst" name="sgst" readonly>

Here is a rewrite of YOUR script using YOUR html

If you have SETS of fields, wrap each set in a div and use .closest("div").find(".otherfield") to make this work in for example a shopping list

const $qty = $(".qty2").on("input", () => { // cache the fields
  let total = $qty
    .map((i, fld) => +fld.value)
    .get()
    .reduce((a, b) => a + b);
  let tot_price = (total * 9 / 100).toFixed(2);
  $("#subtotal").val(total);
  $('#cgst').val(tot_price);
  $('#sgst').val(tot_price);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control price qty2" required name="price[]">
<input type="text" class="form-control price qty2" required name="price[]">
<input type="text" class="form-control price qty2" required name="price[]">
<input type="text" class="form-control qty1" id="subtotal" name="subtotal" readonly>
<input type="text" class="form-control qty1" id="cgst" name="cgst" readonly>
<input type="text" class="form-control qty1" id="sgst" name="sgst" readonly>

梦中的蝴蝶 2025-02-19 22:54:53

只需在代码中的#subtototal行上评论变更事件,就可以了。

$(document).on("change", ".qty2", function() {
  var sum1 = 0;
  $(".qty2").each(function() {
    sum1 += +$(this).val();
  });
  $("#subtotal").val(sum1);
// });

// $(document).on("change", "#subtotal", function() {

  var subt = $("#subtotal").val();

  var tot_price = (subt * 9 / 100);
  var divobj = document.getElementById('cgst');
  var divobj1 = document.getElementById('sgst');
  divobj.value = tot_price;
  divobj1.value = tot_price;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control qty1" id="subtotal" required name="subtotal" readonly>
<input type="text" class="form-control qty1" id="cgst" required name="cgst" readonly>
<input type="text" class="form-control qty1" id="sgst" required name="sgst" readonly>

just comment on change event on #Subtotal lines in your code and you are good to go.

$(document).on("change", ".qty2", function() {
  var sum1 = 0;
  $(".qty2").each(function() {
    sum1 += +$(this).val();
  });
  $("#subtotal").val(sum1);
// });

// $(document).on("change", "#subtotal", function() {

  var subt = $("#subtotal").val();

  var tot_price = (subt * 9 / 100);
  var divobj = document.getElementById('cgst');
  var divobj1 = document.getElementById('sgst');
  divobj.value = tot_price;
  divobj1.value = tot_price;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control price qty2" id="client_type" required name="price[]">
<input type="text" class="form-control qty1" id="subtotal" required name="subtotal" readonly>
<input type="text" class="form-control qty1" id="cgst" required name="cgst" readonly>
<input type="text" class="form-control qty1" id="sgst" required name="sgst" readonly>

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