欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javascript 年月日联动实现核心代码

程序员文章站 2023-10-29 13:00:52
复制代码 代码如下:var startyear = 1980; var endyear = 2500; function monthandday() { this.init...
复制代码 代码如下:

var startyear = 1980;
var endyear = 2500;

function monthandday()
{
this.initddl = function(objyear,objmonth,objday,hidyear,hidmonth,hidday)
{
this.init(objyear,objmonth,objday,hidyear,hidmonth,hidday);
this.selectchange(objyear,objmonth,objday,hidyear,hidmonth,hidday);
}

this.init = function(objyear,objmonth,objday,hidyear,hidmonth,hidday)
{
var year = document.getelementbyid(objyear);
var month = document.getelementbyid(objmonth);
var day = document.getelementbyid(objday);
var me=this;

if(year.length == 1)
{
year.options[0] = new option("不限","");
for(var i = startyear; i < endyear; i++)
{
year.options[year.length] = new option(i,i);
}
}
me.yearoptionschange(document.all[objyear].value,objyear,objmonth,objday);
me.monthoptionschange(document.all[objyear].value,document.getelementbyid(objmonth).value,objday);
}

this.yearoptionschange = function(selectvalue,objyear,objmonth,objday)
{
var month = document.getelementbyid(objmonth);
var day = document.getelementbyid(objday);
var me=this;
month.length = 0;
day.length = 0;
month.options[0]=new option("不限",'');
day.options[0]=new option("不限",'');

if(selectvalue == "")
{
return ;
}

for(var i = 1; i < 13; i++)
{
month.options[month.length] = new option((i < 10 ? ("0" + i) : i),i);
}
me.monthoptionschange(document.getelementbyid(objyear).value,document.getelementbyid(objmonth).value,objday);
}

this.monthoptionschange = function(yearselectvalue,monthselectvalue,objday)
{
var day = document.getelementbyid(objday);
var endday;
day.length = 0;

day.options[0]=new option("不限",'');

if(monthselectvalue == "")
{
return;
}
if((parseint(yearselectvalue) % 400 == 0) || (parseint(yearselectvalue) % 4 == 0 && parseint(yearselectvalue) % 100 != 0))
{
if(parseint(monthselectvalue) == "2")
{
endday = 29;
}
}
else
{
if(parseint(monthselectvalue) == "2")
{
endday = 28;
}
}
switch(parseint(monthselectvalue))
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
endday = 31;
break;
case 4:
case 6:
case 9:
case 11:
endday = 30;
break;
}

for(var i = 1; i <= endday; i++)
{
day.options[day.length] = new option((i < 10 ? ("0" + i) : i),i);
}
}

this.selectchange = function(objyear,objmonth,objday,hidyear,hidmonth,hidday)
{
var year = document.all[objyear];
var month = document.all[objmonth];
var day = document.all[objday];
var me = this;

year.onchange = function()
{
me.yearoptionschange(this.options[this.selectedindex].value,objyear,objmonth,objday);
document.all[hidyear].value = this.options[this.selectedindex].value;
document.all[hidmonth].value = "";
document.all[hidday].value = "";
}

month.onchange = function()
{
me.monthoptionschange(document.all[hidyear].value,this.options[this.selectedindex].value,objday);
document.all[hidmonth].value = this.options[this.selectedindex].value;
document.all[hidday].value = "";
}

day.onchange = function()
{
document.all[hidday].value = day.selectvalue;
}
}
}