简体   繁体   English

如何使用 javascript 在 foreach 中实时显示数组输入的总计

[英]How to display live the Total of an array input inside the foreach using javascript

I have this form which is in a foreach loop, to get the value of the amount input "qty[]" it should be multiply to "uprice[]".我有这种形式,它在一个 foreach 循环中,要获取输入“qty []”的数量的值,它应该乘以“uprice []”。 and the last input that is outside the foreach loop, it should display the total sum of all amount.以及在 foreach 循环之外的最后一个输入,它应该显示所有金额的总和。

<tbody>
     <?php foreach($_POST['porder'] as $porder): ?>
    <tr>                    
        <td>
        <input type="text" id="itemnum[]" name="itemnum[]" 
            max=999 class="form-control form-control-line">
        </td>
        <td>
        <input type="text" id="code[]" name="code[]" class="form-control form-control-line" 
            value="<?php echo $porder ; ?>"readonly>
        </td>
        <td>
        <input style="text-transform:uppercase"  type="text" id="rev" 
            name="rev[]" class="form-control form-control-line" required>
        </td>
        <td>
        <input  style="text-transform:uppercase" type="text" id="desc" 
            name="desc[]" class="form-control form-control-line" required>
        </td>
        <td>
        <input type="tel" id="qty<?=$porder?>" name="qty[]" min="1" 
            class="form-control form-control-line" onkeyup="compute('<?=$porder?>')" required>
        </td>
        <td>
        <input type="tel" id="uprice<?=$porder?>" name="uprice[]" min="1" 
            class="form-control form-control-line" onkeyup="compute('<?=$porder?>')" required>
        </td>
        <td>

        <input type="number" id="amount<?=$porder?>" name="amount[]" 
            onkeyup="total()" class="form-control form-control-line" >
        </td>                                           
    </tr>
    <?php endforeach ?>                 
    <tr >
        <td colspan="5" ></td>  
        <td><strong>Total
        </td>
        <td>
        <input type="number" id="total" name="total"  class="form-control" >
        </td>
    </tr>

I tried to use a javascript to display the amount and the total.我尝试使用 javascript 来显示金额和总数。 I only get the amount but not the total.我只得到金额而不是总数。

<script>

function compute(id) {
    var txtFirstNumberValue = document.getElementById('qty'+id).value;
    var txtSecondNumberValue = document.getElementById('uprice'+id).value;
    var result = parseInt(txtFirstNumberValue) * parseInt(txtSecondNumberValue);
if (!isNaN(result)) {
    document.getElementById('amount'+id).value = result;
     }
}

</script>   


<script>
function total(){
    var amount = document.getElementsByName("amount[]");
    var total = 0;  

    for (var i = 0; i <amount.length; i++) {
    var input_value=amount[i];

        var signle_value_input = input_value.value;
        if(signle_value_input.length!=0)
        total +=parseInt(input_value.value);
    }
    if (!isNaN(total)) {
    document.getElementById('total').value = total;
     }
}

</script>

The Total doesn't show.总计不显示。

<script>

function total(){
 var total = 0;
var amount = document.getElementsByName('amount[]');
for (var i = 0; i <amount.length; i++) {
var inp=amount[i];
var signle_value_input = inp.value;
if(signle_value_input.length!=0)
total +=parseInt(inp.value)
}
if (!isNaN(total)) {
document.getElementById("total").value = total;
}
}


</script>

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

相关问题 如何使用JavaScript在输入框中显示总计 - How to display total in input box using javascript Javascript如何在filter()中使用forEach()过滤数组 - Javascript how to filter an array using forEach() inside filter() 如何在 filter() 中使用 forEach() 过滤数组 - Javascript - How to filter an array using forEach() inside filter() - Javascript Javascript / React - 如何使用 forEach 循环内的“select”输入值更新多维数组的值? - Javascript / React - How to update the values of a multidimensional array with the values of a `select` input inside of a forEach loop? 如何使用 javascript 在 label 中求和并显示总数 - How to sum and display total in a label using javascript 如何使用 javascript 在 foreach 中设置一次间隔 - How to setInterval once inside foreach using javascript 如何显示上传文件总数的“实时”计数? - How to display a 'live' count of total files uploaded? 如何从Javascript / Typescript中的数组对象计算运行总计并使用HTML在每个实例中显示输出? - How to calculate running total from an array object in Javascript/Typescript and display output at every instance using HTML? javascript forPromise在数组内部 - javascript forEach on array inside promise 使用Javascript从文本框实时计算总计 - Calculating Total from Textbox live using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM