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

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>三级联动-年月日</title>
		<script type="text/javascript" scr="getDates.js">
		</script>
	</head>
	<body>
		<select name="year" onchange="initDate()">
			<script type="text/javascript">				
				var year=new Date().getFullYear();//在 FF等浏览器内getYear()返回的是 "当前年份-1900" 的值
				for(var i = year - 30;i <= year + 30; i++){
					document.write("<option value=" + i + ">" + i + "</option>");
				}
			</script>
		</select>
		<select name="month" onchange="initDate()">
			<script type="text/javascript">
				for(var i = 1; i <= 12; i++){
					document.write("<option value=" + i + ">" + i + "</option>");
				}
			</script>
		</select>
		<select name="day">
			<script type="text/javascript">
				for(var i = 1; i <= 31; i++){
					document.write("<option value=" + i + ">" + i + "</option>");
				}
			</script>
		</select>
	</body>
</html>


getDates.js

			function  append(o,v){
				var option=new Option(v,v);
				o.options.add(option);
			}
			function  initDate(){ 
				var y = document.getElementsByName("year")[0].value;
				var m = document.getElementsByName("month")[0].value; 
				var day=document.getElementsByName("day")[0];
				day.innerHTML="";
				   
				if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12){
					for(var j = 1; j <= 31; j++){
						append(day,j);
					}
				}
				else if(m==4 || m==6 || m==9 || m==11){
					for(var j = 1; j <= 30; j++){
						append(day,j);
					}
				}
				else if(m == 2){
					var flag = (y % 4 == 0)&& (y % 100 != 0) || (y % 400 == 0);
					if(flag){
						for(var j = 1; j <= 29; j++){
							 append(day,j);
						}
					}
					else{
						for(var j = 1;j <= 28; j++){
							append(day,j);
						}
					}
				}
			}