DIV CSS 佈局教程網

JS 實現出生日期與星座的級聯顯示
編輯:JavaScript綜合知識     

本文示範一個常見表單應用:選擇出生日期(年、月、日)後,星座欄位會自動帶出對應星座,且日的選項會依當月天數動態調整。

JavaScript

function birthdayOnchange(obj) {
  var year = $("<%= DDL_Year.ClientID%>").value;
  if (year == "year") return; else year = parseInt(year, 10);
  var month = $("<%=DDL_Month.ClientID%>").value;
  if (month == "month") return; else month = parseInt(month, 10);
  var day = $("<%=DDL_Day.ClientID%>").value;
  var wholeday = getDays(year, month);
  var options = $("<%=DDL_Day.ClientID%>").options;
  for (var i = 1; i <= wholeday; i++) {
    var j = i.toString();
    j = j.length == 1 ? "0" + j : j;
    options.length = i + 1;
    options[i].value = j;
    options[i].text = j;
    if (day <= wholeday && i == day) { options[i].selected = true; }
  }
}
function getDays(year, month) {
  var dayarr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  if (month == 2) {
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 || year < 1900) return 29;
    else return dayarr[month - 1];
  } else { return dayarr[month - 1]; }
}
function adjustAstro() {
  var v_astro = getAstro($("<%=DDL_Month.ClientID%>").value, $("<%=DDL_Day.ClientID%>").value);
  $("<%=astro.ClientID %>").options[0].text = v_astro;
}
function getAstro(v_month, v_day) {
  v_month = parseInt(v_month, 10);
  v_day = parseInt(v_day, 10);
  if ((v_month == 12 && v_day >= 22) || (v_month == 1 && v_day <= 20)) return "魔羯座";
  else if ((v_month == 1 && v_day >= 21) || (v_month == 2 && v_day <= 19)) return "水瓶座";
  else if ((v_month == 2 && v_day >= 20) || (v_month == 3 && v_day <= 20)) return "雙魚座";
  else if ((v_month == 3 && v_day >= 21) || (v_month == 4 && v_day <= 20)) return "白羊座";
  else if ((v_month == 4 && v_day >= 21) || (v_month == 5 && v_day <= 21)) return "金牛座";
  else if ((v_month == 5 && v_day >= 22) || (v_month == 6 && v_day <= 21)) return "雙子座";
  else if ((v_month == 6 && v_day >= 22) || (v_month == 7 && v_day <= 22)) return "巨蟹座";
  else if ((v_month == 7 && v_day >= 23) || (v_month == 8 && v_day <= 23)) return "獅子座";
  else if ((v_month == 8 && v_day >= 24) || (v_month == 9 && v_day <= 23)) return "處女座";
  else if ((v_month == 9 && v_day >= 24) || (v_month == 10 && v_day <= 23)) return "天秤座";
  else if ((v_month == 10 && v_day >= 24) || (v_month == 11 && v_day <= 22)) return "天蠍座";
  else if ((v_month == 11 && v_day >= 23) || (v_month == 12 && v_day <= 21)) return "射手座";
  return "";
}

HTML

<div>出生日期:</div>
<div>
  <asp:DropDownList ID="DDL_Year" runat="server" onchange="birthdayOnchange(this);"></asp:DropDownList> 年
  <asp:DropDownList ID="DDL_Month" runat="server" onchange="birthdayOnchange(this);adjustAstro();"></asp:DropDownList> 月
  <asp:DropDownList ID="DDL_Day" runat="server" onchange="adjustAstro();"></asp:DropDownList> 日
</div>
<div>星座:</div>
<div>
  <SELECT id=astro disabled name=astro runat="server">
    <OPTION selected>處女座</OPTION>
  </SELECT>
</div>

關鍵在於 getDays 依闰年規則算出當月天數、getAstro 依月日範圍回傳星座,兩者透過 onchange 串接,就能實現「選好生日、星座自動帶出」的級聯效果。

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