年月日のセレクトボックス
たまにはJavaScript。存在しない日付を表示/選択できないようにするライブラリ、要prototype.js。
/* setday.js requires: prototype.js usage: new SetDay('y1', 'm1', 'd1'); */ var SetDay = Class.create(); SetDay.prototype = { y: null, m: null, d: null, initialize: function(y, m, d) { this.y = $(y); this.m = $(m); this.d = $(d); this.y.onchange = this.setDay.bindAsEventListener(this); this.m.onchange = this.setDay.bindAsEventListener(this); }, setDay: function() { var year = parseInt($F(this.y)); var month = parseInt($F(this.m)); var last = this.getLast(year, month); this.d.length = last; for (var day = 1; day <= last; day++) { var idx = day - 1; this.d.options[idx].text = day; this.d.options[idx].value = day; } }, getLast: function(year, month) { var last = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { last[1] = 29; // intercalary day } return last[month - 1]; } };
未実装: 値のゼロパティングとか、セレクトボックス先頭に空白項目を用意するとか。