If the selected attribute is not specified for all option elements under the select element, the first option is selected by default.
You can use select. selectedIndex to obtain the index of the selected option element.
You can use select. options [select. selectedIndex] to obtain the selected option element.
The option element text3 can be used to obtain the value Attribute value of the option element through option. value, that is, value3. You can use option. text to obtain the text in the option element, that is, text3.
If the option element does not define the value attribute, option. value cannot be obtained in IE, but Safari, Opera, and FireFox can still be obtained through option. value, which is the same as option. text.
You can use option. attributes. value & option. attributes. value. specified to determine whether the option element defines the value attribute.
Therefore, the script for obtaining the current select element value is as follows:
Copy to ClipboardReference: [www.bkjia.com] var getSelectValue = funtion (select ){
Var idx = select. selectedIndex,
Option,
Value;
If (idx>-1 ){
Option = select. options [idx];
Value = option. attributes. value;
Return (value & value. specified )? Option. value: option. text );
}
Return null;
}
Original article: http://www.planabc.net/2010/03/27/how_to_get_select_element_value/