指定值“未定义”无法解析或超出范围

发布于 2025-01-17 05:19:26 字数 888 浏览 3 评论 0原文

所以我试图在计费部分制作一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光是把杀猪刀 2025-01-24 05:19:26

对于任何非数字的内容,isNaN 将始终返回 true,而 subTotal[i].innerHTML 永远不会返回 true,因此 sum 将始终为 +0。

if (subTotal[i].id == "lstTtl") {
  const num = parseInt(subTotal[i].innerHTML);
  if(!isNaN(num) {
    sum += num;
  }
}

isNaN will always return true for anything that isn't a number, which subTotal[i].innerHTML never is, therefore sum will always be +0.

if (subTotal[i].id == "lstTtl") {
  const num = parseInt(subTotal[i].innerHTML);
  if(!isNaN(num) {
    sum += num;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文