简体   繁体   English

如何通过JavaScript获取<select>中当前选择的<option>?

[英]How do you get the currently selected <option> in a <select> via JavaScript?

如何通过JavaScript获取当前选择的<select>元素的<option>

This will do it for you: 这将为您做到:

var yourSelect = document.getElementById( "your-select-id" );
alert( yourSelect.options[ yourSelect.selectedIndex ].value )

The .selectedIndex of the select object has an index; select对象的.selectedIndex有一个索引; you can use that to index into the .options array. 您可以使用它来索引.options数组。

var payeeCountry = document.getElementById( "payeeCountry" );
alert( payeeCountry.options[ yourSelect.selectedIndex ].value );

Using the selectedOptions property: 使用selectedOptions属性:

var yourSelect = document.getElementById("your-select-id");
alert(yourSelect.selectedOptions[0].value);

It works in all browsers except Internet Explorer. 适用于除Internet Explorer之外的所有浏览器。

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

相关问题 如何获得dom修改选择框的当前选择选项? - How to get the currently selected option of a dom modified select box? 在html select中获取当前选中的onBlur选项 - Get currently selected option onBlur in html select 如何获得下拉菜单的当前选定值(选择标记) - How do I get the currently selected value of dropdown (select tag) 如何从动态创建的 select 元素中获取所选选项的值? - How do you get the value of the selected option from a dynamically created select element? 如何通过 vanialla JavaScript 获取所选选项的数据属性? - How do I get a data attribute of a selected option via vanialla JavaScript? Javascript:如何使用内部选择来获取数据列表中的选定选项值 - Javascript: how to get selected option value that is in the datalist with inside select 如何将样式应用于select中当前选择的选项元素? - How to apply style to currently selected option element in select? 你如何禁用一个<option>在另一个选择<select>在申请新增加的同时<select>和以前<select> - How do you disable an <option> selected in another <select> while applying to newly added <select> and previous <select> 如何检查当前选定选项的值(使用 Javascript) - How to check the value of the currently selected option (using Javascript) 如何通过JavaScript选择选择标签的选项? - How to select option of select tag via JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM