javascript循环不适用于django表单集字段计算,仅计算一个带有id(例如id_forms-0-rate)的表单集字段
这是我模板中的代码,它从输入字段 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要你复杂的代码。
只需要这些行:
Don't need your complicated code.
Just need only these lines: