简体   繁体   English

在 jquery 中的更改不适用于只读输入字段

[英]on change in jquery doesnt work with readonly input field

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这里除了第一个输入字段“价格”之外,所有其他 3 个都是只读的,当用户在价格字段中输入某些内容时,该值被计入小计,从那里我需要计算 sgst 和 cgst 并将其显示在各自的字段中,但是在我的代码中,直到小计显示正常工作,谁能告诉我如何解决这个问题,在此先感谢

Here is a rewrite of YOUR script using YOUR html这是使用您的 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如果您有字段集,请将每个集合包装在一个 div 中并使用 .closest("div").find(".otherfield") 使其适用于例如购物清单

 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>

just comment on change event on #Subtotal lines in your code and you are good to go.只需评论代码中#Subtotal 行上的更改事件,您就可以开始了。

 $(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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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