为什么我在 console.log 上得到的下拉值是“未定义”?

Why am I getting the drop-down value as `undefined' on console.log?

这是我的 select 标签的 html 片段

<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,有人知道为什么会这样吗?

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