- If all option elements under the select element do not specify the selected attribute, the first one will be selected by default.
- The index of the selected option element can be obtained through select.selectedIndex.
- The selected option element can be obtained through select.options[select.selectedIndex].
- option element <option selected="selected" value="value3">text3</option>, the value attribute value of the option element can be obtained through option.value, that is, value3; the text within the option element can be obtained through option.text, that is, text3.
- If the option element does not define a value attribute, option.value cannot be obtained in IE, but Safari, Opera, and FireFox can still obtain it through option.value, and the value 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 to obtain the current select element value is as follows:
var getSelectValue = function(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;
}