开发者

javascript get value from dropdown

开发者 https://www.devze.com 2023-01-17 17:49 出处:网络
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.

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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消