簡體   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