コードは少し難しいですが、次のようにうまく機能します。2つの同様の関数を作成しました。最初の関数は、3つの選択ボックスすべてを初期化するためにonloadと呼ばれ、2番目の関数はonchangeイベントが発生したときに呼び出されます。
function init_dates() {
var now = new Date();
var newest = [];
newest[0] = now.getFullYear();
newest[1] = now.getMonth() + 1;
newest[2] = now.getDate();
var old = new Date(now.getTime() - 1095 * 86400 * 1000);
var oldest = [];
oldest[0] = old.getFullYear();
oldest[1] = old.getMonth() + 1;
oldest[2] = old.getDate();
// YEARS
var opt;
var i = oldest[0];
var year = document.getElementById('year');
while (i <= newest[0]) {
opt = document.createElement('option');
opt.value = i;
opt.innerHTML = i;
if (i == newest[0]) {
opt.selected = 'selected';
}
year.appendChild(opt);
i++;
}
// MONTHS
var minmonth = (year.value == oldest[0]) ? oldest[1] : 1;
var maxmonth = (year.value == newest[0]) ? newest[1] : 12;
var month = document.getElementById('month');
while (minmonth <= maxmonth) {
opt = document.createElement('option');
opt.value = minmonth;
opt.innerHTML = minmonth;
month.appendChild(opt);
minmonth++;
}
// DAYS
var minday = (year.value == oldest[0] && month.value == oldest[1]) ? oldest[2] : 1;
var maxday = (year.value == newest[0] && month.value == newest[1]) ? newest[2] : 33;
if (maxday == 33) {
// find correct maxday
maxday = 27;
var d = new Date(year.value, month.value - 1, maxday, 0, 0, 0, 0);
while (d.getDate() > 1) {
maxday++;
d = new Date(year.value, month.value - 1, maxday, 0, 0, 0, 0);
}
maxday--;
}
var day = document.getElementById('day');
while (minday <= maxday) {
opt = document.createElement('option');
opt.value = minday;
opt.innerHTML = minday;
day.appendChild(opt);
minday++;
}
}
function update_dates() {
var now = new Date();
var newest = [];
newest[0] = now.getFullYear();
newest[1] = now.getMonth() + 1;
newest[2] = now.getDate();
var old = new Date(now.getTime() - 1095 * 86400 * 1000);
var oldest = [];
oldest[0] = old.getFullYear();
oldest[1] = old.getMonth() + 1;
oldest[2] = old.getDate();
//selected date in selectboxes
var year = document.getElementById('year');
var month = document.getElementById('month');
var day = document.getElementById('day');
var sel = new Date(year.value, month.value-1, day.value, 0,0,0,0);
var selected = [];
selected[0] = sel.getFullYear();
selected[1] = sel.getMonth() + 1;
selected[2] = sel.getDate();
year.innerHTML = month.innerHTML = day.innerHTML = ''; //clear old list
// YEARS
var opt;
var i = oldest[0];
while (i <= newest[0]) {
opt = document.createElement('option');
opt.value = i;
opt.innerHTML = i;
if (i == selected[0]) {
opt.selected = 'selected';
}
year.appendChild(opt);
i++;
}
// MONTHS
var minmonth = (year.value == oldest[0]) ? oldest[1] : 1;
var maxmonth = (year.value == newest[0]) ? newest[1] : 12;
while (minmonth <= maxmonth) {
opt = document.createElement('option');
opt.value = minmonth;
opt.innerHTML = minmonth;
if (minmonth== selected[1]) {
opt.selected = 'selected';
}
month.appendChild(opt);
minmonth++;
}
// DAYS
var minday = (year.value == oldest[0] && month.value == oldest[1]) ? oldest[2] : 1;
var maxday = (year.value == newest[0] && month.value == newest[1]) ? newest[2] : 33;
if (maxday == 33) {
// find correct maxday
maxday = 27;
var d = new Date(year.value, month.value - 1, maxday, 0, 0, 0, 0);
while (d.getDate() > 1) {
maxday++;
d = new Date(year.value, month.value - 1, maxday, 0, 0, 0, 0);
}
maxday--;
}
while (minday <= maxday) {
opt = document.createElement('option');
opt.value = minday;
opt.innerHTML = minday;
day.appendChild(opt);
if (minday == selected[2]) {
opt.selected = 'selected';
}
minday++;
}
}
http://jsfiddle.net/gCDXs/1/