birthday.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. (function($){
  2. $.extend({
  3. ms_DatePicker: function (options) {
  4. var defaults = {
  5. YearSelector: "#sel_year",
  6. MonthSelector: "#sel_month",
  7. DaySelector: "#sel_day",
  8. FirstText: "请选择",
  9. FirstValue: ''
  10. };
  11. var opts = $.extend({}, defaults, options);
  12. var $YearSelector = $(opts.YearSelector);
  13. var $MonthSelector = $(opts.MonthSelector);
  14. var $DaySelector = $(opts.DaySelector);
  15. var FirstText = opts.FirstText;
  16. var FirstValue = opts.FirstValue;
  17. // 初始化
  18. var str = "<option value=\"" + FirstValue + "\">" + FirstText + "</option>";
  19. $YearSelector.html(str);
  20. $MonthSelector.html(str);
  21. $DaySelector.html(str);
  22. // 年份列表
  23. var yearNow = new Date().getFullYear();
  24. var resYear = yearNow + 5;
  25. var yearSel = $YearSelector.attr("rel");
  26. for (var i = resYear; i >= 2010; i--) {
  27. var sed = yearSel==i?"selected":"";
  28. var yearStr = "<option value=\"" + i + "\" " + sed+">" + i + "</option>";
  29. $YearSelector.append(yearStr);
  30. }
  31. // 月份列表
  32. var monthSel = $MonthSelector.attr("rel");
  33. for (var i = 1; i <= 12; i++) {
  34. var sed = monthSel==i?"selected":"";
  35. var monthStr = "<option value=\"" + i + "\" "+sed+">" + i + "</option>";
  36. $MonthSelector.append(monthStr);
  37. }
  38. // 日列表(仅当选择了年月)
  39. function BuildDay() {
  40. if ($YearSelector.val() == 0 || $MonthSelector.val() == 0) {
  41. // 未选择年份或者月份
  42. $DaySelector.html(str);
  43. } else {
  44. $DaySelector.html(str);
  45. var year = parseInt($YearSelector.val());
  46. var month = parseInt($MonthSelector.val());
  47. var dayCount = 0;
  48. switch (month) {
  49. case 1:
  50. case 3:
  51. case 5:
  52. case 7:
  53. case 8:
  54. case 10:
  55. case 12:
  56. dayCount = 31;
  57. break;
  58. case 4:
  59. case 6:
  60. case 9:
  61. case 11:
  62. dayCount = 30;
  63. break;
  64. case 2:
  65. dayCount = 28;
  66. if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
  67. dayCount = 29;
  68. }
  69. break;
  70. default:
  71. break;
  72. }
  73. var daySel = $DaySelector.attr("rel");
  74. for (var i = 1; i <= dayCount; i++) {
  75. var sed = daySel==i?"selected":"";
  76. var dayStr = "<option value=\"" + i + "\" "+sed+">" + i + "</option>";
  77. $DaySelector.append(dayStr);
  78. }
  79. }
  80. }
  81. $MonthSelector.change(function () {
  82. BuildDay();
  83. });
  84. $YearSelector.change(function () {
  85. BuildDay();
  86. });
  87. if($DaySelector.attr("rel")!=""){
  88. BuildDay();
  89. }
  90. } // End ms_DatePicker
  91. });
  92. })(jQuery);