JS未来14天下降

时间:2018-02-08 10:44:12

标签: javascript forms

我正在尝试创建一个下拉列表,通过JS显示未来14天。 现在它只显示7天,但有重复。

有什么问题?

屏幕截图下拉列表

screenshot dropdown

function createDates() {
    $("#DT").find('option').remove().end().append('<option value="" selected>~#SELECT_DT~</option>');
    var counter = 0;
    var index = 1;
    console.log(holidays);
    while (counter < 14) {
        var date = new Date();
        date.setDate(date.getDate() + index);
        var day = date.getDay();
        var month = date.getMonth() + 1;
        var isWeekend = (day == 6) || (day == 0);
        var checkTime1 = getTimeOfDate(date.getFullYear(), month, date.getDate());
        var isFound = false;
        var selected = "";
        if (index == ~(IF(@DT<>'',@DT,0))~) {
            selected = 'selected="selected"';
        }
        if (!isWeekend) {
            if (holidays.length > 0) {
                $.each(holidays, function(key, obj) {
                    var hDay = new Date(obj.DT);
                    var checkTime2 = getTimeOfDate(hDay.getFullYear(), hDay.getMonth() + 1, hDay.getDate());
                    if (checkTime1 == checkTime2) {
                        isFound = true;
                    }
                });
            } else {
                $("#DT").append('<option value="' + index + '"' + selected + '>' + getDayString(day) + ' ' + date.getDate() + ' ' + getMonthString(month) + ' ' + date.getFullYear() + '</option>');
                counter++;
            }
            if (!isFound) {
                $("#DT").append('<option value="' + index + '"' + selected + '>' + getDayString(day) + ' ' + date.getDate() + ' ' + getMonthString(month) + ' ' + date.getFullYear() + '</option>');
                counter++;
            }
        }
        index++;
    }
}

1 个答案:

答案 0 :(得分:0)

第二个if块应该在第一个下面,同时在开头设置isFound为false:

    if (!isWeekend) {
        isFound = false;
        if (holidays.length > 0) {
            $.each(holidays, function(key, obj) {
                var hDay = new Date(obj.DT);
                var checkTime2 = getTimeOfDate(hDay.getFullYear(), hDay.getMonth() + 1, hDay.getDate());
                if (checkTime1 == checkTime2) {
                    isFound = true;
                }
            });
            if (!isFound) {
                $("#DT").append('<option value="' + index + '"' + selected + '>' + getDayString(day) + ' ' + date.getDate() + ' ' + getMonthString(month) + ' ' + date.getFullYear() + '</option>');
                counter++;
            }
        } else {
            $("#DT").append('<option value="' + index + '"' + selected + '>' + getDayString(day) + ' ' + date.getDate() + ' ' + getMonthString(month) + ' ' + date.getFullYear() + '</option>');
            counter++;
        }
    }