【问题标题】:Does not execute the code after a for within a function不执行函数内的 for 之后的代码
【发布时间】:2015-01-02 10:27:38
【问题描述】:

好!我有一个问题,你没有在循环结束时运行我的代码,上面和循环内部的东西都可以正常工作,问题是循环之后仍然没有执行代码。知道为什么会这样吗?

这是我的代码:

var arrayp = new Array();
function botonAdelante(tabl, pasos)
{
  var padreTabla = document.getElementById(tabl).rows;
  var cont = 0;

  for(var j = 0; j < padreTabla.length; j++)
  {
    var hijoTd = document.getElementById(pasos+ "-producto-" +j);
    var childArray = hijoTd.children;
    for(var i = 0; i < childArray.length; i++)
    {
      var check = document.getElementById(pasos+ "-CheckBox-" +j);
      if(check.type == 'checkbox' && check.checked==true)
      {
        arrayp[cont] = check.value;
        var algo = arrayp[cont];

        alert(arrayp[cont]);
        alert(arrayp);

        cont++;
        continue;
      };
    }
  }
  alert("It is in this part of the code does not work");
}

澄清:在long的末尾找到“继续”,如果它也不起作用。

【问题讨论】:

  • 继续的目的是什么?什么没有执行?警报?
  • 没错,不工作的是警报
  • 你有两个循环。哪个不工作?另外,您的意思是break; 而不是continue;
  • 两个循环工作,不工作的是警报,然后定位两个循环。把它继续,因为我在网上找到它,但如果它仍然不起作用
  • 我建议在调试器中逐步执行此操作,并确定它停止执行的确切位置。我很确定发生了一个错误,导致脚本停止。

标签: javascript function iteration


【解决方案1】:

这样使用 continue 令人困惑,但我感觉您的代码可能会抛出错误,因为 cont 可能超过数组长度。不管这是否修复它,我至少会添加一个检查以确保它不会引发异常。

请检查通过网络开发工具(Chrome 中的 F12)抛出的异常。

        for(var i = 0; i < childArray.length; i++)
        {
            var check = document.getElementById(pasos+ "-CheckBox-" +j);
            if(check.type == 'checkbox' && check.checked==true && arrayp.length <= cont)
            {
                arrayp[cont] = check.value;
                var algo = arrayp[cont];

                alert(arrayp[cont]);
                alert(arrayp);

                cont++;
                continue;
            };
        }

【讨论】:

  • Eh 在 if 中添加了条件,但没有奏效。现在可以调试了,但不是很好。
  • 我认为问题是这样的:hijoTd var = document.getElementById (steps + "-products-" + j);我认为在 for 你不能创建自动 id 用“j”增加的变量
  • 你也应该检查 dom 元素上的空值。
  • 我检查是否检查,但我认为它会迭代循环,因为它不会覆盖在 getElementById (pasos + "-products-" + j) 中找到的“j”的值;跨度>
猜你喜欢
  • 1970-01-01
  • 2016-07-07
  • 2018-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-10
  • 2018-12-16
相关资源
最近更新 更多