首页 > 代码库 > js时间日期的三级联动
js时间日期的三级联动
aspx:
<div style="width:70px;float:left;">生日</div> <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>年 <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>月 <asp:DropDownList ID="DropDownList3" runat="server"></asp:DropDownList>日
js:
<script>
//年份变动的事件 document.getElementById("DropDownList1").onchange = function () { var mon = document.getElementById("DropDownList2"); var day = document.getElementById("DropDownList3"); if (mon.value =http://www.mamicode.com/= "2") { if (this.value % 4 == 0 && this.value % 100 != 0 || this.value % 400 == 0) { day.options.length = 0; for (var i = 1; i < 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } else if (mon.value =http://www.mamicode.com/= "1" || mon.value =http://www.mamicode.com/= "3" || mon.value =http://www.mamicode.com/= "5" || mon.value =http://www.mamicode.com/= "7" || mon.value =http://www.mamicode.com/= "8" || mon.value =http://www.mamicode.com/= "10" || mon.value =http://www.mamicode.com/= "12") { day.options.length = 0; for (var i = 1; i <= 31; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i <= 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } //月变动的事件 document.getElementById("DropDownList2").onchange = function () { var day = document.getElementById("DropDownList3"); var year = document.getElementById("DropDownList1"); if (this.value =http://www.mamicode.com/= "2") { if (year.value % 4 == 0 && year.value % 100 != 0 || year.value % 400 == 0) { day.options.length = 0; for (var i = 1; i < 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i < 29; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } else if (this.value =http://www.mamicode.com/= "1" || this.value =http://www.mamicode.com/= "3" || this.value =http://www.mamicode.com/= "5" || this.value =http://www.mamicode.com/= "7" || this.value =http://www.mamicode.com/= "8" || this.value =http://www.mamicode.com/= "10" || this.value =http://www.mamicode.com/= "12") { day.options.length = 0; for (var i = 1; i <= 31; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } else { day.options.length = 0; for (var i = 1; i <= 30; i++) { var op = document.createElement("option"); op.value = i; op.innerHTML = i; day.appendChild(op); } } } </script>
后台cs:
if (!IsPostBack) { for (int i = DateTime.Now.Year; i >= 1900; i--) { ListItem li = new ListItem(i.ToString(), i.ToString()); DropDownList1.Items.Add(li); } for (int i = 1; i < 13; i++) { DropDownList2.Items.Add(new ListItem(i.ToString(), i.ToString())); } for (int i = 1; i < 31; i++) { DropDownList3.Items.Add(new ListItem(i.ToString(), i.ToString())); } }
js时间日期的三级联动
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。