在html5 select中获取所选选项的键(文本)

时间:2019-02-12 12:15:32

标签: javascript reactjs html5

我正在选择一个字段。我想访问它的键(显示的文本)。目前正在运行,但感觉有点愚蠢。

<select onChange={
  (e) => {
    console.log('e.target', e.target);
    onChangeEvent(e.target.options[event.target.options.selectedIndex].text, e.target.value);
}}>
  ...
</select>

还有什么更好的

  

e.target.options [event.target.options.selectedIndex] .text

不使用jQuery,其他模块...

1 个答案:

答案 0 :(得分:2)

如果您不关心IE:

const select = document.querySelector('.select')

select.addEventListener('change', (e) => {
  const selected = e.target.selectedOptions[0]
  console.log(selected.text, selected.value)
})
<select class="select">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

相关问题