2010-09-22 237 views
0

我有一個php腳本,它的數組循環遍歷數月,並在下拉菜單中顯示它們。我想使用javascript從下拉列表中選擇值。javascript從下拉列表中獲取值

function month_list() 
{ 


$months = Array("January", "February", "March", "April", "May", "June", "July", 
"August", "September", "October", "November", "December"); 

echo '<select name="month" id="month_list" OnChange="getvalue();">'; 

foreach ($months as $months => $month) 
{ 
     echo'<option OnChange="getvalue();" value='.$month.'>'.$month.'</option>'; 

    } 

echo '</select>'; 

} 

的Javascript:當我選擇一個值

<script type="text/javascript"> 


function getvalue() 
{ 
alert(document.getElementById((month_list)).value); 
} 

</script> 

沒有happends但螢火我得到的錯誤:

Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead. 

有什麼建議?

謝謝。

回答

1

你忘了引用month_list。

document.getElementById("month_list").value 

而且你不需要平變化(這是所有小寫BTW)屬性上的各個選項,只選擇元素。

+0

謝謝!噢,我忘了我把那部分留在了:) – Elliott 2010-09-22 20:19:00

1

使用

function getvalue() 
{ 
alert(document.getElementById('month_list').value); 
} 

</script> 

相反。

此外,您可以刪除每個選項的onChange,選擇將做:)

相關問題