【问题标题】:JQUERY for loop not outputting integers within the arrayJQUERY for循环不输出数组中的整数
【发布时间】:2021-02-01 23:22:29
【问题描述】:

我在下面尝试了这段代码,但循环的记录输出显示为3649 ,这不是正在传递的数组中的 ID 号。即使在数组中添加更多 id 或减去 id 时,也不会改变 ajax 代码的输出。

我已使用console.log(id) 确认下面的函数可以正确接收数组。这验证输出如下:

传递的数组:

 console.log(cids);
(3) […]
​
0: "45"
​
1: "47"
​
2: "46"
​
length: 3

我可能做错了什么?

function func2(cids) {
    console.log(cids);
      for(let id of cids)
        var parent = document.getElementById('mydiv_' + id)
        console.log(id);
      }
}

如果我 console.log(id); 我得到 3649,它甚至不在数组中

【问题讨论】:

  • 提供的代码似乎不足以帮助解决此问题,但我建议减少问题以帮助隔离它。例如,可以绕过 ajax 调用并硬编码一个 cid 变量,您可以将其传递给 callD()。一旦您确定该函数存在/不存在问题,您可以将 ajax 部分重新合并,看看是否仍能获得预期的结果。
  • @chrissmith 谢谢。这是我拥有的唯一代码。正如 OP 中提到的,我已经验证了该数组正在正确地进入第二个函数,所以我不知道还能做什么。我确实只是编辑了代码以使其更少
  • 变量中到底有什么...... console.log(JSON.stringify(cids));

标签: javascript jquery for-loop


【解决方案1】:

您缺少一个花括号。试试:

function func2(cids) {
    console.log(cids);
    for(let id of cids){ //this one is missing
        var parent = document.getElementById('mydiv_' + id)
        console.log(id);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多