Javascript 如何使用事件侦听器从select标记中获取所选文本?

Javascript 如何使用事件侦听器从select标记中获取所选文本?,javascript,Javascript,HTML <select id='alpha'> <option value='1'>one</option> <option value='2'>two</option> </select> 我正在获取所选值,但如何获取所选文本 我试过谷歌,但什么也没找到。 那么,如何使用事件侦听器获取所选文本呢?您需要访问下拉菜单的selectedIndex中的下拉菜单的选项: var select_node=document

HTML

<select id='alpha'>
  <option value='1'>one</option>
  <option value='2'>two</option>
</select>
我正在获取所选值,但如何获取所选文本 我试过谷歌,但什么也没找到。

那么,如何使用事件侦听器获取所选文本呢?

您需要访问下拉菜单的
selectedIndex
中的下拉菜单的
选项:

var select_node=document.getElementById(“alpha”);
选择_node.addEventListener('change',(e)=>{
console.log(e.target.options[e.target.selectedIndex].text);//获取所选值。
});
试试:

属性收集所有的
s

属性返回所选

.text
属性当然是
text

或者这里有一个更简单的方法:

e.target.selectedOptions[0].text;
属性是
.options
.selectedIndex

const select_node=document.getElementById('alpha');
选择_node.addEventListener('change',(e)=>{
console.log(e.target.value);//获取所选值。
console.log(e.target.options[e.target.selectedIndex].text);//获取所选文本。
console.log(e.target.selectedOptions[0].text);//以更简单的方式获取所选文本。
},假)

一
二

value
如何返回所选文本?如果需要内容而不是value属性中的内容,请将.value更改为.text。已相应更新。谢谢,但我想使用e参数获取选择文本。然后使用e.target而不是所选节点:e.target.options[e.target.selectedIndex]。text@marks请帮帮我,也请帮帮我@Abhishekkamal这些答案能解决你的问题吗?如果是,请接受一个。
e.target.options[e.target.selectedIndex].text
e.target.selectedOptions[0].text;