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

时间:2021-05-28

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章