时间联动(Jquery)
时间联动(Jquery)
<form name='form1' id='form1'>
<table>
<tr>
<td class="tRight" >出生日期:</td>
<td class="tLeft" >
<select name='yyyy' onchange="YYYYMM(this.value)">
<option value="0">请选择 年</option>
</select>
<select name='mm' onchange="MMDD(this.value)">
<option value="0">选择 月</option>
</select>
<select name='dd'>
<option value="0">选择 日</option>
</select>
</td>
</tr>
</table>
</form>
<script src='./jquery.js'></script>
<script language="JavaScript"><!--
$(function() {
var fo = document.form1
strY = fo.yyyy;
strM = fo.mm;
strD = fo.dd;
MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var date = new Date();
//先给年下拉框赋内容
var y = date.getFullYear();
var str = "";
for (var i = (y); i > (y-70); i--)
{
str += "<option value='" + i + "'> " + i + " 年" + "</option>";
}
$(strY).html(str);
//赋月份的下拉框
var str = '';
for (var i = 1; i < 13; i++)
{
str += "<option value='" + i + "'> " + i + " 月" + "</option>\r\n";
}
$(strM).html(str);
var n = MonHead[date.getMonth()];
if (date.getMonth ==1 && IsPinYear(y)) n++;
writeDay(n); //赋日期下拉框
//此处是为了解决IE6不兼容问题(jQuery select 无法设置selected属性。未指明的错误)
setTimeout(function() {
$(strY).val(y);
$(strM).val(date.getMonth()+1);
$(strD).val(date.getDate());
},1);
})
function YYYYMM(str) //年发生变化时日期发生变化(主要是判断闰平年)
{
var MMvalue = document.form1.mm.options[document.form1.mm.selectedIndex].value;
if (MMvalue == ""){dd.outerHTML = strD; return;}
var n = MonHead[MMvalue - 1];
if (MMvalue ==2 && IsPinYear(str)) n++;
writeDay(n)
}
function MMDD(str) //月发生变化时日期联动
{
var YYYYvalue = document.form1.yyyy.options[document.form1.yyyy.selectedIndex].value;
if (str == ""){dd.outerHTML = strD; return;}
var n = MonHead[str - 1];
if (str ==2 && IsPinYear(YYYYvalue)) n++;
writeDay(n)
}
function writeDay(n) //据条件写日期的下拉框
{
var s = '';
for (var i=1; i<(n+1); i++)
s += "<option value='" + i + "'> " + i + " 日" + "</option>";
$(strD).html(s);
}
function IsPinYear(year)//判断是否闰平年
{
return(0 == year%4 && (year%100 !=0 || year%400 == 0))
}
//-->
</script>
//原代码取于网络,这里修改了第一个方法(改为Jquery) 并解决了IE6不兼容问题。