0

目前我有一组日期字符串,这是唯一的

eg.

2012 04 01
2012 04 10
2012 04 25
2012 06 30
2012 06 10
2012 12 01
2013 04 01

我需要做的是创建 3 个选择框,一个代表年,一个代表月,一个代表日

问题是我需要维护年月日之间的关系

例如,首先用户可以查看 2012 2013,然后如果 2012, 04, 06, 12 显示为月份,如果选择 04 作为月份,则 01 , 10, 25 显示为日期

实现是:

                for (var key in data) {
                    year = key.substring(0,4);
                    if ($.inArray(year, yearArr) == '-1'){
                        yearArr.push(year);
                        $('#year').append('<option value="' + year + '">' + year + '</option>');
                    }
                    dateArr.push(key);
                    chtml.push("<tr><td>" + key + "</td><td>" + data[key] + "</td></tr>");
                }

其中 Key 是每个日期字符串 abd dateArr 存储的所有日期

$("#year").change(function(){
    var month;
    var monthArr = [];
    var selectedValue = $(this).find(":selected").val();
    if (selectedValue !== '-1') {
        $('#month').prop("disabled", false);
        $('#month').empty().append('<option value="-1">Please choose</option>');
        for (var key in dateArr) {
            if (selectedValue == key.substring(0,4)) {
                month = key.substring(4,2);
                if ($.inArray(month, monthArr) == '-1'){
                    monthArr.push(month);
                    $('#month').append('<option value="' + month + '">' + month + '</option>');
                }
            }
        }
    } else {
        $('#month,#day').empty().append('<option value="-1">Please choose</option>');
    }
});

$("#month").change(function(){
    var day;
    var dayArr = [];
    var selectedValue = $("#year").find(":selected").val() + $(this).find(":selected").val();
    if (selectedValue !== '-1') {
        $('#day').prop("disabled", false);
        $('#day').empty().append('<option value="-1">Please choose</option>');
        for (var key in dateArr) {
            if (selectedValue == key.substring(0,6)) {
                day = key.substring(6,2);
                if ($.inArray(day, dayArr) == '-1'){
                    dayArr.push(day);
                    $('#day').append('<option value="' + day + '">' + day + '</option>');
                }
            }
        }
    } else {
        $('#month,#day').empty().append('<option value="-1">Please choose</option>');
    }
});

When select box year or month is change, append the correspond value, but that is not working because when user change month, they can back to change the year, how to fix the problem? 或者推荐任何更简单的方法来做到这一点?谢谢

4

0 回答 0