I have a php script with an array which loops through months and displays them in a drop down. I want to get the selected value from the drop down using 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/jav开发者_运维问答ascript">
function getvalue()
{
alert(document.getElementById((month_list)).value);
}
</script>
Nothing happends when I select a value but in firebug I get the error:
Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
Any advice?
Thanks.
You forgot to quote month_list.
document.getElementById("month_list").value
Also you don't need the onchange (that's all lowercase btw) attributes on the individual options, just on the select element.
Use
function getvalue()
{
alert(document.getElementById('month_list').value);
}
</script>
Instead.
Also, you can remove the onChange on each of the options, the select will do :)
精彩评论