DIV CSS 佈局教程網

年/月/日三層連動下拉選單:閏年判斷與 ASP.NET 取值
編輯:JavaScript綜合知識     

出生日期選擇器是表單裡的常客,看似簡單卻有個經典難題:二月的天數會變。平年 28 天、閏年 29 天,而月份天數又有「七前單大、八後雙大」的規則——一月、三月、五月、七月、八月、十月、十二月為 31 天,四月、六月、九月、十一月為 30 天。要做對連動,就得把這些規則寫進程式。

頁面結構

三個 select 加上一個隱藏欄位與送出按鈕:

<select name="YYYY" id="YYYY" onchange="YYYYMM(this.value)">
  <option value="">選擇 年</option>
</select>
<select name="MM" id="MM" onchange="MMDD(this.value)">
  <option value="">選擇 月</option>
</select>
<select name="DD" id="DD">
  <option value="">選擇 日</option>
</select>

<asp:HiddenField ID="hfValue" runat="server" />
<asp:Button ID="btnSave" runat="server" Text="保存" onclick="btnSave_Click" />

初始化:動態產生年與月

頁面載入時,以 outerHTML 重建 select 的內容。年份以今年為基準,往前 80 年、往後 1 年:

var MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

window.onload = function() {
    strYYYY = document.form1.YYYY.outerHTML;
    strMM   = document.form1.MM.outerHTML;
    strDD   = document.form1.DD.outerHTML;

    // 產生年份選項
    var y = new Date().getFullYear();
    var str = strYYYY.substring(0, strYYYY.length - 9);
    for (var i = (y - 80); i < (y + 2); i++) {
        str += "<option value='" + i + "'>" + i + " 年</option>";
    }
    document.form1.YYYY.outerHTML = str + "</select>";

    // 產生月份選項
    var str = strMM.substring(0, strMM.length - 9);
    for (var i = 1; i < 13; i++) {
        str += "<option value='" + i + "'>" + i + " 月</option>";
    }
    document.form1.MM.outerHTML = str + "</select>";

    // 預設為今天
    document.form1.YYYY.value = y;
    document.form1.MM.value   = new Date().getMonth() + 1;

    var n = MonHead[new Date().getMonth()];
    writeDay(n);
    document.form1.DD.value = new Date().getDate();
};

連動邏輯與閏年判斷

年改變或月改變時,都要重新計算「日」可以選到幾號:

// 年變化時
function YYYYMM(str) {
    var MMvalue = document.form1.MM.options[document.form1.MM.selectedIndex].value;
    if (MMvalue == "") { DD.outerHTML = strDD; 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 = strDD; return; }

    var n = MonHead[str - 1];
    if (str == 2 && IsPinYear(YYYYvalue)) n++;
    writeDay(n);
}

// 依指定天數重建日選單
function writeDay(n) {
    var s = strDD.substring(0, strDD.length - 9);
    for (var i = 1; i < (n + 1); i++) {
        s += "<option value='" + i + "'>" + i + " 日</option>";
    }
    document.form1.DD.outerHTML = s + "</select>";
}

// 閏年判斷:四年一閏、百年不閏、四百年再閏
function IsPinYear(year) {
    return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0));
}

IsPinYear 這行是整個範例的技術核心:單純「能被 4 整除就是閏年」會讓 1900 年誤判為閏年,而 2000 年才是閏年,因此必須加上 100 與 400 的例外規則。

取出選定值

送出前把三個 select 的值組合成 YYYY-MM-DD 寫入隱藏欄位,後端就能直接讀取:

function getValue() {
    var year  = document.getElementById("YYYY").value;
    var month = document.getElementById("MM").value;
    var day   = document.getElementById("DD").value;
    document.getElementById("hfValue").value = year + "-" + month + "-" + day;
}
// .aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
    btnSave.Attributes.Add("onclick", "getValue()");
}

protected void btnSave_Click(object sender, EventArgs e)
{
    Response.Write(hfValue.Value);
}

建議的改進方向

原範例用 outerHTML 反覆重建 select,這做法有幾個問題:會破壞已綁定的事件與選中狀態,在部分瀏覽器上 outerHTML 的讀寫行為也不一致,而且 substring(0, length - 9) 這種依賴字串長度的寫法相當脆弱。
更好的做法是保留 select 元素,只操作 options 集合,或用 DocumentFragment 一次建好再掛上;若要支援現代瀏覽器,直接用 new Option(text, value) 加入選項即可。另外,月份與日期建議補零成 01~12,後端處理日期格式時會省事很多。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved