关于JS时间三级联动的初始化问题!
大家帮忙看看,年份的初始化怎么没效果呢! 求高手解决呀! 代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>js</title>
<style type="text/css">
</style>
</head>
<body>
<select id="year" onchange="initDay()">
<option>请选择年份</option>
</select>
<select id="month" onchange="initDay()">
<option>请选择月份</option>
</select>
<select id="day">
<option>请选择日</option>
</select>
</body>
</html>
<script type="text/javascript">
//初始化年份
var year=new Date().getYear();
for(var i=year;i>=1990;i--)
{
document.getElementById("year").options[year+1-i]=new Option(i,i);
}
//初始化月份
for(var i=1;i<=12;i++)
{
document.getElementById("month").options[i]=new Option(i,i);
}
//初始化日
function initDay()
{
//添加之前先初始化
document.getElementById("day").length=1;
var year=document.getElementById("year").value;
var month=document.getElementById("month").value;
if(year==""||month=="")
{
return;
}
else
{
var arr=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
if((year%4==0&&year%100!=0)||year%400==0)
{
arr[1]++;
}
for(i=1;i<=arr[month-1];i++)
{
document.getElementById("day").options[i]=new Option(i,i);
}
}
}
</script>
------解决方案--------------------
var year = new Date().getFullYear();
用这个