JS实现动态的日期选择表单,使用方法:
showDateSelect("date1")
其中参数为表单项select的名称,然后可以用date1.value获取选择的日期.
<html>
<head>
<title> 动态的日期选择表单 </title>
<meta http-equiv="Content-Type" content="text/html" charset="gb2312">
<script language="JavaScript">
<!--
function yearList(num){
var d=new Date();
var t=d.getFullYear();
var yStr="";
for(var i=t-num;i<=t+num;i++){
yStr=yStr+"<option value='"+i+(i==t?"' selected>":"'>")+i+"</option>";
}
return yStr;
}
function monthList(){
var d=new Date();
var t=d.getMonth()+1;
mStr="";
for(var i=1;i<=12;i++){
mStr=mStr+"<option value='"+i+(i==t?"' selected>":"'>")+i+"</option>";
}
return mStr;
}
function dateList(fName){
var d=new Date();
var t=document.all[fName+"_dd"].value==""?d.getDate():document.all[fName+"_dd"].value;
d=new Date(document.all[fName+"_yy"].value+"/"+document.all[fName+"_mm"].value+"/1");
dStr="";
for(var i=1;i<=31;i++){
d.setDate(i);
if(d.getDate()!=i){break;}
dStr=dStr+"<option value='"+i+(i==t?"' selected>":"'>")+i+"</option>";
}
document.all[fName+"_dx"].innerHTML="<select name='"+fName+"_dd' οnchange='javascript:setDateValue(/""+fName+"/");'>"+dStr+"</select>";
}
function setDateValue(name){
var dValue=document.all[name+"_yy"].value+"-"+document.all[name+"_mm"].value+"-"+document.all[name+"_dd"].value;
document.all[name].value=dValue;
}
function showDateSelect(name){
document.write("<select name='"+name+"_yy' οnchange='javascript:dateList(/""+name+"/");setDateValue(/""+name+"/");'>"+yearList(10)+"</select>");
document.write("<select name='"+name+"_mm' οnchange='javascript:dateList(/""+name+"/");setDateValue(/""+name+"/");'>"+monthList()+"</select>");
document.write("<span id='"+name+"_dx'><select name='"+name+"_dd'>"+"</select></span>");
dateList(name);
document.write("<input type='hidden' name='"+name+"' value=''>");
setDateValue(name);
}
//-->
</script>
</head>
<body>
<center><h3>动态日期选择表单示例</h3></center>
<table align="center" width="300" cellspacing="1" cellpadding="5" border >
<FORM METHOD=POST ACTION="" οnsubmit="alert('date1:'+this.date1.value+'/ndate2:'+this.date2.value+'/ndate3:'+this.date3.value);return false;">
<tr>
<td>日期1: <script language="JavaScript">showDateSelect("date1");</script></td>
</tr>
<tr>
<td>日期2: <script language="JavaScript">showDateSelect("date2");</script></td>
</tr>
<tr>
<td>日期3: <script language="JavaScript">showDateSelect("date3");</script></td>
</tr>
<tr>
<td><CENTER><INPUT TYPE="submit" value="SUBMIT"></CENTER></td>
</tr>
</FORM>
</table>
</body>
</html>