js時間日期的三級聯動
阿新 • • 發佈:2017-05-22
this innerhtml end 後臺 run reat func 時間 dropdown
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 == "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 == "1" || mon.value == "3" || mon.value == "5" || mon.value == "7" || mon.value == "8" || mon.value == "10" || mon.value == "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 == "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 == "1" || this.value == "3" || this.value == "5" || this.value == "7" || this.value == "8" || this.value == "10" || this.value == "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時間日期的三級聯動