Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用另一个Jquery函数触发一个Jquery函数_Javascript_Jquery - Fatal编程技术网

Javascript 用另一个Jquery函数触发一个Jquery函数

Javascript 用另一个Jquery函数触发一个Jquery函数,javascript,jquery,Javascript,Jquery,在下面的表格行中,我在.quantity中输入时进行计算,计算结果放在.amount2中。这工作得很好,正如您在下面看到的那样 <tr class="manifest-row"> <td width = 17.5% class="productCode" onchange="populateProduct(this)">{{for

在下面的表格行中,我在
.quantity
中输入时进行计算,计算结果放在
.amount2
中。这工作得很好,正如您在下面看到的那样

                    <tr class="manifest-row">
                      <td width = 17.5% class="productCode" onchange="populateProduct(this)">{{form.ProductCode}}</td>
                      <td width = 32.5% class="description">{{form.DescriptionOfGoods}}</td>
                      <td width = 12.5% class="quantity" oninput="calculateUnit(this)">{{form.UnitQty}}</td>
                      <td width = 12.5% class="unitType">{{form.Type}}</td>
                      <td width = 10.5% class="price" oninput="calculate(this)">{{form.Price}}</td>
                      <td width = 12.5% class="amount2">{{form.Amount}}</td>
                    
                    </tr>
问题是可以有很多行,我有一个单独的函数,它对类为
.amount2
的输入中的所有值求和,并将该和放在字段
\id\u InvoiceTotal
中。但是下面的函数在输入时不会正确触发,因为它是由上面的函数填充的。那么,如何使上面的函数触发下面的函数呢?我在我的在线搜索中看到了
.trigger()
,但我不知道如何在这里应用它

<script>
$(document).on("input", ".amount2", function() {
  var total = 0;
  var i = 0;
  $('#form_set .amount2').each(function() {

    total += parseInt($('#id_form-'+(i)+'-Amount').val());
    i++;
    $('#id_InvoiceTotal').val(total);
  })
});

</script>

$(document).on(“input”,“.amount2”,function()){
var合计=0;
var i=0;
$('#form_set.amount2')。每个(函数(){
total+=parseInt($('#id_form-'+(i)+'-Amount').val();
i++;
$('id#u InvoiceTotal').val(总计);
})
});

您可以在同一个函数中执行此操作,只需使用以下两个附加语句:

function calculateUnit(el) {
  let total = 0;
  var row = el.closest("tr");
  var unitQty = el.querySelector('.quantity input').value;
  var price = row.querySelector('.price input').value;

  var lineTotal = unitQty * price;

  row.querySelector('.amount2 input').value = lineTotal;

  document.querySelectorAll(".amount2 input").forEach(function(item){
    total += item.value / 1;
  });

  document.querySelector("#id_InvoiceTotal").value = total;

}

只需将其移动到一个函数,然后从
calculateUnit()调用它即可。谢谢,这是一个很好的答案,但问题是,由于这是在输入时发生的,因此当用户退格时,
total
会被添加到函数中。例如,如果数量为10,价格为10,则金额最初为100。如果我将数量中的0退格,它将加上10(1 x 10),并将其添加到总数中。我真的很抱歉。我错过了。我已经更新了code.hm,现在它只返回0。就我所知,你能详细说明你想做什么吗?基本上,当你改变
.quantity
元素中的输入时,代码中的函数将被触发<代码>总计
将在每次调用时使用
0
初始化。我添加了一个forEach循环,它将遍历所有
.amount输入
元素,并将它们的值添加到
total
@GXM100,我刚刚检查了它。实际上有一个印刷错误。我用
.amount
代替了
.amount2
。现在可以了,我已经更新了代码。
function calculateUnit(el) {
  let total = 0;
  var row = el.closest("tr");
  var unitQty = el.querySelector('.quantity input').value;
  var price = row.querySelector('.price input').value;

  var lineTotal = unitQty * price;

  row.querySelector('.amount2 input').value = lineTotal;

  document.querySelectorAll(".amount2 input").forEach(function(item){
    total += item.value / 1;
  });

  document.querySelector("#id_InvoiceTotal").value = total;

}