javascript从下拉列表中获取值
我有一个带有数组的 php 脚本,该数组循环数月并将它们显示在下拉列表中。我想使用 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>
当我选择一个值时没有任何反应,但在 Firebug 中我收到错误:
Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
有什么建议吗?
谢谢。
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/javascript">
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您忘记引用month_list。
此外,您不需要各个选项上的 onchange (顺便说一句,都是小写)属性,只需在 select 元素上即可。
You forgot to quote month_list.
Also you don't need the onchange (that's all lowercase btw) attributes on the individual options, just on the select element.
使用
反而。
另外,您可以删除每个选项上的 onChange,选择即可:)
Use
Instead.
Also, you can remove the onChange on each of the options, the select will do :)