2009-12-15 99 views

回答

8

很簡單:

options[selectedIndex].id 
11

未做太多的假設(即選擇是一個有效的選擇元素),

var options = select.options; 
var id  = options[options.selectedIndex].id; 
var value = options[options.selectedIndex].value; 

,或者

var options = select.options; 
var value = (options.selectedIndex != -1) ? options[selectedIndex].value : null; 
var id  = (options.selectedIndex != -1) ? options[selectedIndex].id : null; 

始終檢查虛假(或值爲)。例2將變量設置爲null(如果沒有選擇任何內容)。