0

再会,

我正在尝试创建一个日期选择器,其中所有字段(YMD)都是下拉菜单,并自动选择月份影响在日期字段中可以选择的天数。

我不希望以任何方式显示日历。

感谢任何关于做什么的建议。

谢谢。

4

2 回答 2

3

这可能会有所帮助。

   $(function () {
            $("#monthSelect").on("change", function () {
                BindDays();
            });
            $("#yearSelect").on("change", function () {
                BindDays();
            });
            BindDays();
        });

        function BindDays() {
            var month = $("#monthSelect").val();
            var dayDropDown = $("#daySelect");
            dayDropDown.empty();
            if (month == 2) {
                for (var i = 1; i <= 28; i++) {
                    dayDropDown.append("<option value=" + i + ">" + i + "</option>");
                }
                var year = $("#yearSelect").val();
                if (parseInt(year) % 4 == 0) {
                    dayDropDown.append("<option value='29'>29</option>");
                }
            }
            else if (month == 4 || month == 6 || month == 9 || month == 11) {
                for (var i = 1; i <= 30; i++) {
                    dayDropDown.append("<option value=" + i + ">" + i + "</option>");
                }
            }
            else {
                for (var i = 1; i <= 31; i++) {
                    dayDropDown.append("<option value=" + i + ">" + i + "</option>");
                }
            }
        }

http://jsfiddle.net/zTTQY/

于 2012-07-20T14:03:59.153 回答
3

您可以从此处下载并包含 jquery datepicker :- 然后在您的页面中包含以下 js

(function()
{

    // initialise the "Select date" link
    $('#date-pick')
        .datePicker(
            // associate the link with a date picker
            {
                createButton:false,
                startDate:(new Date()).asString(),
                endDate:'31/12/2020'
            }
        ).bind(
            // when the link is clicked display the date picker
            'click',
            function()
            {
                updateSelects($(this).dpGetSelected()[0]);
                $(this).dpDisplay();
                return false;
            }
        ).bind(
            // when a date is selected update the SELECTs
            'dateSelected',
            function(e, selectedDate, $td, state)
            {
                updateSelects(selectedDate);
            }
        ).bind(
            'dpClosed',
            function(e, selected)
            {
                updateSelects(selected[0]);
            }
        );

    var updateSelects = function (selectedDate)
    {
        var selectedDate = new Date(selectedDate);
        $('#d option[value=' + selectedDate.getDate() + ']').attr('selected', 'selected');
        $('#m option[value=' + (selectedDate.getMonth()+1) + ']').attr('selected', 'selected');
        $('#y option[value=' + (selectedDate.getFullYear()) + ']').attr('selected', 'selected');
    }
    // listen for when the selects are changed and update the picker
    $('#d, #m, #y')
        .bind(
            'change',
            function()
            {
                var d = new Date(
                            $('#y').val(),
                            $('#m').val()-1,
                            $('#d').val()
                        );
                $('#date-pick').dpSetSelected(d.asString());
            }
        );

    // default the position of the selects to today
    var today = new Date();
    updateSelects(today.getTime());

    // and update the datePicker to reflect it...
    $('#d').trigger('change');
});

这样,您的日期选择器将转换为选择框,在给定范围内只能选择未来的日期。

供参考:http: //www.kelvinluck.com/assets/jquery/datePicker/v2/demo/datePickerIntoSelects.html

于 2012-07-20T13:55:50.447 回答