如果 select 元素下的所有 option 元素均沒有指定 selected 屬性,會默認選中第一個。
可以通過 select.selectedIndex 獲取到選中的 option 元素的索引。
可以通過 select.options[select.selectedIndex] 獲取到選中的 option 元素。
option 元素 text3,可以通過 option.value 獲得 option 元素的 value 屬性值,即 value3;可以通過 option.text 獲得 option 元素內(nèi)的文本,即 text3。
如果 option 元素沒有定義 value 屬性,則 IE 中 option.value 無法獲得,但 Safari、Opera、FireFox 依舊可以通過 option.value 獲得,值同于 option.text 。
可以通過 option.attributes.value && option.attributes.value.specified 來判斷 option 元素是否定義了 value 屬性。
故,獲得當前 select 元素值的腳本如下:
原文:http://www.planabc.net/2010/03/27/how_to_get_select_element_value/