WEB开发网
开发学院网页设计JavaScript JavaScript 一个日期选择函数 阅读

JavaScript 一个日期选择函数

 2009-05-06 20:12:11 来源:WEB开发网   
核心提示:<select id="Year" name="Year"><option value=""></option></select>年<select id="Month" name=&qu

<select id="Year" name="Year"><option value=""></option></select>年
<select id="Month" name="Month"></select>月
<select id="Day" name="Day"></select>日<br />

<select id="iYear" name="iYear"><option value=""></option></select>年
<select id="iMonth" name="iMonth"></select>月<select id="iDay" name="iDay"></select>日

<script language="javascript">
<!--

//使用方法(对象区别字符串,默认选中日期,时间段),无顺序要求
PPDayselect("");
PPDayselect("i","1988-8-8","1988-1999");

function PPDayselect(){var str=["","1940-2009","0-0-0"];for(var i=0;i<arguments.length;i++){if(arguments[i].split("-").length==1){str[0]=arguments[i]};if(arguments[i].split("-").length==2){str[1]=arguments[i]};if(arguments[i].split("-").length==3){str[2]=arguments[i]};}str[1]=str[1].split("-");if(str[2]=="0-0-0"){str[2]=str[1][0]+"-01-01";}str[2]=str[2].split("-");str[1][0]=parseInt(str[1][0]);str[1][1]=parseInt(str[1][1]);if(str[2][1].length<2){str[2][1]="0"+str[2][1];}if(str[2][2].length<2){str[2][2]="0"+str[2][2];}PPDayselect1(str[0]+"Year",str[1][0],str[1][1]-str[1][0]+1);PPDayselect1(str[0]+"Month",1,12);document.getElementById(str[0]+"Year").onchange=function(){PPDayselect2(str[0]);};document.getElementById(str[0]+"Month").onchange=function(){PPDayselect2(str[0]);};document.getElementById(str[0]+"Year").value=str[2][0];document.getElementById(str[0]+"Month").value=str[2][1];PPDayselect2(str[0]);document.getElementById(str[0]+"Day").value=str[2][2];
}
function PPDayselect1(ObjectName,StartNum,Length){var o=document.getElementById(ObjectName);o.options.length=Length;var ii;for(var i=0;i<Length;i++){ii=i+StartNum;if(ii<10){ii="0"+ii;}o.options[i]=new Option(ii,ii);}}

function PPDayselect2(str){var DayLength=[31,28,31,30,31,30,31,31,30,31,30,31];var Year=document.getElementById(str+"Year").value;var Month=document.getElementById(str+"Month");if(Year%4==0){DayLength[1]=29;}else{DayLength[1]=28;}PPDayselect1(str+"Day",1,DayLength[Month.selectedIndex]);}  
//-->
</script>

Tags:JavaScript 一个 日期

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接