javascript - For 循环重复第一个循环两次

标签 javascript loops

我有这个 for 循环,它似乎重复第一个循环两次 (x=0),然后不执行最后一个循环 (x=2)

for (x=0;x<=2;x++)
    {
        if (document.getElementById("sub"+catCount+x).value != "")
        {
            if (nonums.test(document.getElementById("sub"+catCount+x).value))
            {
                total = total + parseFloat(document.getElementById("sub"+catCount+x).value);
            }
        }
        alert(x);
    }

换句话说,我收到两个带有“0”的警报框,然后是一个带有“1”的警报框,仅此而已。

谁能告诉我我在这里没有看到什么?为什么它不正常地通过循环(0,1,2)?

最佳答案

that is literally the only spot I use the variable x on any page.

It works for me .

for (x=0;x<=2;x++)
{
  alert(x);
}

您可以在控制台测试它。

关于javascript - For 循环重复第一个循环两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3121670/

相关文章:

java - 如何防止我的变量永远持续下去?

javascript - 如何在 Javascript 中创建数组的数组?

无法离开循环

algorithm - 我如何优化 O(n^2)

javascript - 对启用 Keycloak 的 Angular 应用程序进行单元测试

javascript 使用 cookie 将变量传递到另一个页面

javascript - XUL Firefox 插件中的 appendChild 中断

ios - 更新 UILabel 看起来像是在浪费性能

javascript - reactjs-toolbox 单选按钮组不改变

javascript - 如果在 JavaScript 或 jQuery 中选择了另一个表单字段,如何仅显示表单字段?