日期:2014-05-16  浏览次数:20439 次

出生年月日--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=gb2312" />
<title>年月日联动下拉菜单</title>

<script>
function YearMonthDay(){
	var fo=document.getElementById("ModifyPwdAccessAction_action");
   foday=fo.day;
   MonHead=[31,28,31,30,31,30,31,31,30,31,30,31];
  
   //add options for year
   y=new Date().getFullYear();
   for(i=y;i>=(y-100);i--)
   fo.year.options.add(new Option(i,i));
   fo.year.options.value=y;//current year
  
    //add options for month
   m=new Date().getMonth();
   for(i=1;i<=12;i++)
   fo.month.options.add(new Option(i,i));
   fo.month.options.value=m+1;//current month
  
   //add options for day
   d=new Date().getDay();
   n=MonHead[m];
   if(m==1&&IsRunYear(fo.year.options.value))
   n++;
   day(n);
   fo.day.options.value=d+1;//curren day
}

   //onchange of year
function yy(str){
	var fo=document.getElementById("ModifyPwdAccessAction_action");
   monthValue=fo.month.options[fo.month.selectedIndex].value;
   if(monthValue==""){
      var foday=fo.day;
   optionClear(foday);
   return;
    }
   var n=MonHead[monthValue-1];
   if(monthValue==2&&IsRunYear(str)) n++;
   day(n);
}

   //onchange of month
function mm(ab){
	var fo=document.getElementById("ModifyPwdAccessAction_action");
  yearValue=fo.year.options[fo.year.selectedIndex].value;
 if(yearValue==""){
  optionClear(foday);
  return;
 }
 var n=MonHead[ab-1];
 if(ab==2&&IsRunYear(yearValue)) n++;
 day(n);
}
  
function day(ab){
   optionClear(foday);
   for(var i=1;i<=ab;i++)
   foday.options.add(new Option(i,i));
 }
 
function optionClear(ab){
   for(var i=ab.options.length;i>0;i--)
   ab.remove(i);
}

function  IsRunYear(year){ 
   return(0==year%4&&(year%100!=0 || year%400==0));
}
</script>
?

</head>
<body onload="YearMonthDay()">
<form name=form1>
? <select name=year onchange="yy(this.value)">
??? <option?? value=""></option>
? </select>
? <select name=month onchange="mm(this.value)">
??? <option?? value=""></option>
? </select>
? <select name=day>
??? <option?? value=""></option>
? </select>
</form>
</body>
</html>