当前位置:Gxlcms > JavaScript > javascript 年月日联动实现核心代码

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

时间:2021-07-01 10:21:17 帮助过:2人阅读

代码如下:
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;
}
}
}

人气教程排行