【问题标题】:How to iterate through multiple select options using forEach to get innerText values如何使用 forEach 遍历多个选择选项以获取 innerText 值
【发布时间】:2020-06-02 17:39:17
【问题描述】:

您如何迭代,使用 forEach(或类似的现代浏览器方法)来获取每个选择选项的 innerText 值?这会引发错误。 Uncaught TypeError: intervals.forEach is not a function

let intervals = document.querySelector("#edit_amenity_hour_interval").options;
intervals.forEach(interval => {
    console.log(interval.innerText)
})

【问题讨论】:

  • 你也可以粘贴你的html代码吗?
  • document.querySelector("#edit_amenity_hour_interval") 放置一个console.log 看看是否有option 可迭代

标签: javascript ecmascript-6 foreach


【解决方案1】:

使用Array.from,因为选项返回集合,而不是数组

let intervals = Array.from(document.querySelector("#edit_amenity_hour_interval").options)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-02
    • 1970-01-01
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多