gpt4 book ai didi

javascript - 如何使用 Javascript 从输入框值中获取总和?

转载 作者:可可西里 更新时间:2023-11-01 01:26:45 24 4
gpt4 key购买 nike

我在 Javascript 方面并不完美。我想在不刷新页面的情况下,在名为 total 的下一个输入框中显示在 qty 输入框中输入的值的总和。谁能帮我弄清楚..?

这是javascript

 <script type="text/javascript"> 
var howmanytoadd = 2;
var rows;

function calc() {
var tot = 0;
for (var i = 0; i < rows.length; i++) {
var linetot = 0;
rows[i].getElementsByTagName('input')[howmanytoadd].value = linetot;
tot += linetot;
}
document.getElementById('total').value = tot
}
onload = function () {
rows = document.getElementById('tab').getElementById('qty1');
for (var i = 0; i < rows.length; i++) {
rows.getElementsByTagName('input')[i].onkeyup = calc;
}
}
</script>

这是我的 html 代码:

Qty1 : <input type="text" name="qty1" id="qty"/><br>
Qty2 : <input type="text" name="qty2" id="qty"/><br>
Qty3 : <input type="text" name="qty3" id="qty"/><br>
Qty4 : <input type="text" name="qty4" id="qty"/><br>
Qty5 : <input type="text" name="qty5" id="qty"/><br>
Qty6 : <input type="text" name="qty6" id="qty"/><br>
Qty7 : <input type="text" name="qty7" id="qty"/><br>
Qty8 : <input type="text" name="qty8" id="qty"/><br>
<br><br>
Total : <input type="text" name="total" id="total"/>

这是截图 here is screen shot

最佳答案

尝试:

Qty1 : <input onblur="findTotal()" type="text" name="qty" id="qty1"/><br>
Qty2 : <input onblur="findTotal()" type="text" name="qty" id="qty2"/><br>
Qty3 : <input onblur="findTotal()" type="text" name="qty" id="qty3"/><br>
Qty4 : <input onblur="findTotal()" type="text" name="qty" id="qty4"/><br>
Qty5 : <input onblur="findTotal()" type="text" name="qty" id="qty5"/><br>
Qty6 : <input onblur="findTotal()" type="text" name="qty" id="qty6"/><br>
Qty7 : <input onblur="findTotal()" type="text" name="qty" id="qty7"/><br>
Qty8 : <input onblur="findTotal()" type="text" name="qty" id="qty8"/><br>
<br><br>
Total : <input type="text" name="total" id="total"/>


<script type="text/javascript">
function findTotal(){
var arr = document.getElementsByName('qty');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
document.getElementById('total').value = tot;
}

</script>

关于javascript - 如何使用 Javascript 从输入框值中获取总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13540751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com