转载+整理:
Code
<script language ="javascript" type ="text/javascript">
//for birthday
function YYYYMMDDstart()
{
MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
//先给年下拉框赋内容
var y = new Date().getFullYear();
for (var i = (y-100); i < (y+1); i++) //以今年为准,前30年,后30年
year.options.add(new Option(" "+ i +" 年", i));
//赋月份的下拉框
for (var i = 1; i < 13; i++)
month.options.add(new Option(" " + i + " 月", i));
year.value = y;
month.value = new Date().getMonth() + 1;
var n = MonHead[new Date().getMonth()];
if (new Date().getMonth() ==1 && IsPinYear(YYYYvalue)) n++;
writeDay(n); //赋日期下拉框
day.value = new Date().getDate();
}
function YYYYDD(str) //年发生变化时日期发生变化(主要是判断闰平年)
{
var MMvalue = month.value;
if (MMvalue == ""){ var e = day; optionsClear(e); return;}
var n = MonHead[MMvalue - 1];
var dayValue = day.value;
if (MMvalue ==2 && IsPinYear(str)) n++;
writeDay(n);
//如果选择的日期在这个范围内那么重新设置这个日期
if (dayValue <= n);
day.value = dayValue;
}
function MMDD(str) //月发生变化时日期联动
{
var YYYYvalue = year.value;
if (YYYYvalue == ""){ var e = day; optionsClear(e); return;}
var n = MonHead[str - 1];
var dayValue = day.value;
if (str ==2 && IsPinYear(YYYYvalue)) n++;
writeDay(n);
if (dayValue <= n);
day.value = dayValue;
}
function writeDay(n) //据条件写日期的下拉框
{
var e = day; optionsClear(e);
for (var i=1; i<(n+1); i++)
e.options.add(new Option(" "+ i + " 日", i));
}
function IsPinYear(year)//判断是否闰平年
{
return(0 == year%4 && (year%100 !=0 || year%400 == 0));
}
function optionsClear(e)
{
e.options.length = 1;
}
</script>
html代码:
<td class="font_R">生日</td>
<td>
<select id="ddlYear" runat="server" onchange="YYYYDD(this.value)">
<option value="" selected="selected">请选择 年</option>
</select>
<select id="ddlMonth" runat ="server" onchange="MMDD(this.value)">
<option value="" selected="selected">选择 月</option>
</select>
<select id="ddlDay" runat="server">
<option value="" runat ="server" selected="selected">选择 日</option>
</select>
</td>
……
var year, month, day;
year = document.getElementById('<%=ddlYear.ClientID %>');
month = document.getElementById('<%=ddlMonth.ClientID %>');
day = document.getElementById('<%=ddlDay.ClientID %>');
YYYYMMDDstart();
"<script type='text/javascript' language ='javascript' >"+
"year.value={0};YYYYDD({0});year.value={0};month.value={1};day.value={2}</script>", dt.Year.ToString(), dt.Month.ToString(), dt.Day.ToString());