javascript循环不适用于django表单集字段计算,仅计算一个带有id(例如id_forms-0-rate)的表单集字段

发布于 2025-01-13 06:02:28 字数 1568 浏览 0 评论 0原文

这是我模板中的代码,它从输入字段 ID 中获取值并计算该值。代码 仅适用于一个表单集字段或项目,例如 id= id_form-0-rate 的字段或项目。我不知道为什么循环 不适用于添加的表单集,例如 id= id_form-1-rate 的表单集。

template.html

 <script type = "text/javascript" >
        var id= $("#id_form-TOTAL_FORMS").val();
        console.log(id); 
        for(let i=0; i<10; i++)
        {
            console.log(i);
        $("#id_form-" + i + "-quantity").keyup(function () {
            var rate = parseFloat($("#id_form-" + i + "-rate").val());
            var quantity = parseFloat($("#id_form-" + i + "-quantity").val());
            var discount = parseFloat($("#id_form-" + i + "-discount").val());
            $("#id_form-" + i + "-amount").val(rate * quantity - discount);
        });
        $("#id_form-" + i + "-rate").keyup(function () {
            var rate = parseFloat($("#id_form-" + i + "-rate").val());
            var quantity = parseFloat($("#id_form-" + i + "-quantity").val());
            var discount = parseFloat($("#id_form-" + i + "-discount").val());
            $("#id_form-" + i + "-amount").val(rate * quantity - discount);
        });
        $("#id_form-" + i + "-discount").keyup(function () {
            var rate = parseFloat($("#id_form-" + i + "-rate").val());
            var quantity = parseFloat($("#id_form-" + i + "-quantity").val());
            var discount = parseFloat($("#id_form-" + i + "-discount").val());
            $("#id_form-" + i + "-amount").val(rate * quantity - discount);
        });
        }
</script>

This is the code in my template it takes value from input field id and calculate the value. The code
only works for one formset field or item such as one having id= id_form-0-rate. I dont know why loop
doesnot work for added formset such as one having id= id_form-1-rate.

template.html

 <script type = "text/javascript" >
        var id= $("#id_form-TOTAL_FORMS").val();
        console.log(id); 
        for(let i=0; i<10; i++)
        {
            console.log(i);
        $("#id_form-" + i + "-quantity").keyup(function () {
            var rate = parseFloat($("#id_form-" + i + "-rate").val());
            var quantity = parseFloat($("#id_form-" + i + "-quantity").val());
            var discount = parseFloat($("#id_form-" + i + "-discount").val());
            $("#id_form-" + i + "-amount").val(rate * quantity - discount);
        });
        $("#id_form-" + i + "-rate").keyup(function () {
            var rate = parseFloat($("#id_form-" + i + "-rate").val());
            var quantity = parseFloat($("#id_form-" + i + "-quantity").val());
            var discount = parseFloat($("#id_form-" + i + "-discount").val());
            $("#id_form-" + i + "-amount").val(rate * quantity - discount);
        });
        $("#id_form-" + i + "-discount").keyup(function () {
            var rate = parseFloat($("#id_form-" + i + "-rate").val());
            var quantity = parseFloat($("#id_form-" + i + "-quantity").val());
            var discount = parseFloat($("#id_form-" + i + "-discount").val());
            $("#id_form-" + i + "-amount").val(rate * quantity - discount);
        });
        }
</script>

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

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

发布评论

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

评论(1

独闯女儿国 2025-01-20 06:02:28

不需要你复杂的代码。
只需要这些行:

<script>
$(document).on('keyup',"input[id$='-quantity']",fn);
$(document).on('keyup',"input[id$='-rate']",fn);
$(document).on('keyup',"input[id$='-discount']",fn);
function fn(){
   var rate = parseFloat($(this).closest("#parent").find("input[id$='-rate']").val());
   var quantity = parseFloat($(this).closest("#parent").find("input[id$='-quantity']").val());
   var discount = parseFloat($(this).closest("#parent").find("input[id$='-discount']").val());
   $(this).closest("#parent").find("input[id$='-amount']").val(rate * quantity - discount);
};
</script>

{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
<div id="parent">
   {{ form.as_p }}
</div>
{% endfor %}

Don't need your complicated code.
Just need only these lines:

<script>
$(document).on('keyup',"input[id$='-quantity']",fn);
$(document).on('keyup',"input[id$='-rate']",fn);
$(document).on('keyup',"input[id$='-discount']",fn);
function fn(){
   var rate = parseFloat($(this).closest("#parent").find("input[id$='-rate']").val());
   var quantity = parseFloat($(this).closest("#parent").find("input[id$='-quantity']").val());
   var discount = parseFloat($(this).closest("#parent").find("input[id$='-discount']").val());
   $(this).closest("#parent").find("input[id$='-amount']").val(rate * quantity - discount);
};
</script>

{% csrf_token %}
{{ formset.management_form }}
{% for form in formset %}
<div id="parent">
   {{ form.as_p }}
</div>
{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文