-2

我有 3 个下拉列表框,yearmonthday

我需要填充从当前日期开始的最后 1095 天的值。目前,我通过在带有日历系统表的 SQL 中运行查询来做到这一点。有没有办法直接在javascript中做到这一点?

我使用 SQL 的原因是从今天开始的 1095 天内我无法关联 3 个下拉框。例如,假设今天是 2009 年 9 月 6 日。如果我为 选择 2009 year,那么month下拉列表应该从 9 月开始,day下拉列表应该从 6 开始。

我需要用户能够灵活地选择过去 1095 天中的任何日期。所以首先用户选择 2009 .only 然后应该填充月份下拉框。它应该只有从 9 月到 12 月的月份。然后选择月份为9 月只有 1 到 30 应该填充在天下拉框中。我目前正在通过使用 sql 中的系统日期表来执行此操作。

4

1 回答 1

0

代码有点困难,但恕我直言,以下效果很好。做了两个类似的函数,第一个调用onload初始化所有三个选择框,第二个在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/

于 2012-09-07T16:42:10.527 回答