[英]getting the value of the selected DropDown menu option
So i know there have been many posts about this and ive read them, but the solutions recommended in this previous posts, dont work. 因此,我知道有很多关于此的文章,我已经阅读了,但是在以前的文章中推荐的解决方案不起作用。
I have a function that dynamically creates me a drop down menu. 我有一个可以动态创建下拉菜单的功能。 Here it is: 这里是:
function date(start, end) {
output ="";
for(i=start;i<=end;i++)
{
output += "<option value='"+i+"'>"+i+"</option>";
}
document.write(output);
}
And i call it like this: 我这样称呼它:
Month:
<select name="mm" id="mm" disabled onchange="activateDate();">
<script type="text/javascript">
date(1,12);
</script>
</select>
Note: activateDay just changes the attribute disabled in the next dropdown menu, because you should first select a year, then a month, then a day. 注意:activateDay只是更改了下一个下拉菜单中禁用的属性,因为您应该首先选择一年,然后是一个月,然后是一天。
and when i try to get the selected value of the month so that i can give a month a number of days (31 for january, 28 for february)like this : 当我尝试获取月份的选定值以便我可以给一个月数天(1月31日,2月28日)时,如下所示:
var a = document.getElementById("mm");
var month = a.options[a.selectedIndex].value;
And if i use this vars for a switch which looks for the month and returns the number of the days, then i give the number of days to my date()
i get an empty drop down menu for my dates. 如果我将此变量用于查找月份并返回天数的开关,则将天数指定为date()
我会得到一个用于日期的空下拉菜单。 The Javascript console in chrome says this: chrome中的Javascript控制台说:
Uncaught TypeError: Cannot read property 'options' of null 未捕获的TypeError:无法读取null的属性“选项”
And points me to this line: 并指出我这一行:
var month = a.options[a.selectedIndex].value;
So what am i doing wrong? 那我在做什么错?
The error 错误
Cannot read property 'options' of null
means that a
is null in a.options[a.selectedIndex].value
. 表示a.options[a.selectedIndex].value
中的a
为null。
Since you have an element with id="mm"
, check if: 由于您具有id="mm"
的元素,请检查是否:
window.document
一些脚本劫持了window.document
document.getElementById
一些脚本劫持了document.getElementById
id
某些脚本已删除您的选择,或更改了其id
var a = document.getElementById("mm")
before loading the select to the DOM 您可以在将选择加载到DOM之前使用var a = document.getElementById("mm")
Probably its the last one, try running your code at onload
event, or place your script just before closing </body>
. 可能是最后一个,尝试在onload
事件上运行代码,或者在关闭</body>
之前放置脚本。
尝试使用此代码
var month = a.options[a.selectedIndex-1].value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.