js实现下拉框联动选中日期(select 日期联动更改)

html代码


<head>
<script type="text/javascript" src="/js/jquery-1.11.1.min.js" ></script>
<script type="text/javascript" src="/js/party.js" ></script>
</head>
<body>
<table style="text-align: right;">
		<tr>
			<td>出生日期:</td>
			<td><select id="birth_year" οnchange="setDays(this,birth_month,birth_day);">
					<option value="">年</option>
					
			</select> <select id="birth_month" οnchange="setDays(birth_year,this,birth_day);">
					<option value="">月</option>
					
			</select> <select id="birth_day">
					<option value="">日</option>
					
			</select></td>
		</tr>
		<tr>
</table>
</body>

js 代码

$(document).ready(function() {
	var i = -1;
	// 添加年份,从1910年开始
	for (i = 1910; i <= new Date().getFullYear(); i++) {
		addOption(birth_year, i, i - 1909);
		/*// 默认选中1988年
	    if (i == 1988) {
	    	birth_year.options[i-1910].selected = true;
	    }*/
	}
	// 添加月份
	for (i = 1; i <= 12; i++) {
	     addOption(birth_month, i, i);
	}
	// 添加天份,先默认31天
	for (i = 1; i <= 31; i++) {
	    addOption(birth_day, i, i);
	}
	
	
	
	//$("#birth_month"). birth_year  birth_day
});

	 
// 设置每个月份的天数
function setDays(year, month,day) {
	var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	var yea = year.options[year.selectedIndex].text;
	var mon = month.options[month.selectedIndex].text;
	var num = monthDays[mon - 1];
	if (mon == 2 && isLeapYear(yea)) {
		num++;
	}
	for (var j = day.options.length - 1; j >=num; j--) {
           day.remove(j);
       }
       for (var i = day.options.length; i <= num; i++) {
           addOption(birth_day,i,i);
       }
}
	 
// 判断是否闰年
function isLeapYear(year)
{
	return (year % 4 == 0 || (year % 100 == 0 && year % 400 == 0));
}
	 
// 向select尾部添加option
function addOption(selectbox, text, value) {
	var option = document.createElement("option");
	option.text = text;
    option.value = value;
    selectbox.options.add(option);
}

原文地址:http://www.cnblogs.com/qingliuyu/archive/2010/10/21/1857586.html

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值