繁体   English   中英

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

[英]Getting the Key (text) of a selected option in a html5 select

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

<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,其他模块...

如果您不关心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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM