【问题标题】:Why am I getting the drop-down value as `undefined' on console.log?为什么我在 console.log 上将下拉值显示为“未定义”?
【发布时间】:2020-02-01 03:41:42
【问题描述】:

这是我的选择标签的 html sn-p

<select id="selDataset"></select>

我已使用以下脚本添加了选项及其对应的值,并已使用检查工具验证它们是否存在

function addOptions() {
    d3.json(url).then(function(data) {
        data.names.forEach((name, i) => {
            var appendOption = selectDropdown.append("option").text(name).attr('value', i)
        })
        selectDropdown.selectedIndex = 0
    })
}

现在我正在尝试获取下拉列表的值!

console.log(selectDropdown.value)

我在控制台上收到undefined,有人知道为什么会这样吗?

【问题讨论】:

    标签: javascript d3.js data-driven


    【解决方案1】:

    selectDropdown 是 d3.js Selection 对象,而不是 HTMLElement。您可以使用selectDropdown.node() 获取元素。 selectDropdown.node().value 是您获取选择元素值的方式。

    【讨论】:

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