JS Select 月日日期联动

时间:2021-04-17 04:21:26

Js对Select控件进行联动操作,一个select选择月份后另一个select生成对应月份的所有日期。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
window.onload = initForm;
function initForm() {
document.getElementById("months").selectedIndex = ;
document.getElementById("months").onchange = populateDays;
}
function populateDays() {
var monthDays = new Array(, , , , , , , , , , , );
var monthStr = this.options[this.selectedIndex].value;
if (monthStr != "") {
var theMonth = parseInt(monthStr);
document.getElementById("days").options.length = ;
for (var i = ; i < monthDays[theMonth]; i++) {
document.getElementById("days").options[i] = new Option(i + );
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select runat="server" id="months">
<option value="">Month</option>
<option value="">January</option>
<option value="">February</option>
<option value="">March</option>
<option value="">April</option>
<option value="">May</option>
<option value="">June</option>
<option value="">July</option>
<option value="">August</option>
<option value="">September</option>
<option value="">October</option>
<option value="">November</option>
<option value="">December</option>
</select>
<select runat="server" id="days">
<option>Day</option>
</select>
</div>
</form>
</body>
</html>