指定值“未定义”无法解析或超出范围
所以我试图在计费部分制作一个基于 POS Web 的应用程序,我试图仅添加由 JavaScript 添加的总列,但它不会添加
HTML: -
<table id="tbl">
<thead>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</thead>
</table>
我创建了一个按钮,可以执行几个操作除此之外,它应该在输入字段中显示总金额,但此 JS 不显示总金额: -
let subTotal = document.getElementById("tbl").getElementsByTagName("td");
let sum;
for (let i = 0; i < subTotal.length; i++) {
if (subTotal[i].id == "lstTtl") {
sum += isNaN(subTotal[i].innerHTML) ? 0 : parseInt(subTotal[i].innerHTML);
}
}
document.getElementById("sTotal").value = sum;
尝试从动态添加的表中获取计算金额。
SO I am trying to make a POS web based app in the billing part I am trying to add the total column only that are added by the JavaScript but it doesn't adds up
HTML: -
<table id="tbl">
<thead>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>Quantity</th>
<th>Total</th>
</thead>
</table>
I have created a button that dose a couple of things other then this, it should show the summed amount in the input field but this JS doesn't shows the summed amount: -
let subTotal = document.getElementById("tbl").getElementsByTagName("td");
let sum;
for (let i = 0; i < subTotal.length; i++) {
if (subTotal[i].id == "lstTtl") {
sum += isNaN(subTotal[i].innerHTML) ? 0 : parseInt(subTotal[i].innerHTML);
}
}
document.getElementById("sTotal").value = sum;
Trying to get a calculated amount from a table that is added dynamically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于任何非数字的内容,
isNaN
将始终返回 true,而subTotal[i].innerHTML
永远不会返回 true,因此 sum 将始终为 +0。isNaN
will always return true for anything that isn't a number, whichsubTotal[i].innerHTML
never is, therefore sum will always be +0.