How do you get the currently selected

2019-01-13 07:25发布

问题:

How do you get the currently selected <option> of a <select> element via JavaScript?

回答1:

This will do it for you:

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


回答2:

The .selectedIndex of the select object has an index; you can use that to index into the .options array.



回答3:

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


回答4:

Using the selectedOptions property:

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

It works in all browsers except Internet Explorer.