【问题标题】:jQuery DatePicker, select and interval of datesjQuery DatePicker,日期的选择和间隔
【发布时间】:2013-07-12 23:48:46
【问题描述】:

我正在尝试选择与所选日期相隔 5 天的日期。

我正在使用 beforeShowDay 方法,但我找不到获取 currentDate 并返回那些日子 ( current + 4 ) 的自定义类的方法。

【问题讨论】:

  • 分享你到目前为止所做的事情
  • 我没有可行的解决方案。我只是不知道如何做到这一点。现在我有一个简单的日期选择器保持打开状态。当我选择一个日期时,我还希望选择接下来的 4 个日期。

标签: jquery jquery-ui jquery-ui-datepicker


【解决方案1】:

我创建了一个jsFiddle,它将展示如何执行此操作。我在这个SO post 中对Yozomiri 的回答很挑剔

请注意,日期选择器实例仍将仅存储当前日期的单击日期。 jQuery Datepicker 被设计为只允许选择一个日期。因此,您必须自己处理存储多个选定日期。

基本上:

$(function () {
    var $date = $('#date').datepicker({
        onSelect: function (dateText, inst) {
            var $picker = inst.dpDiv;
            var $allDates = $picker.find('table.ui-datepicker-calendar td'); // get all date elements

            //This is the important line.
            //Setting this to false prevents the redraw.
            inst.inline = false;

            //The remainder of the function simply preserves the 
            //highlighting functionality without completely redrawing.

            //This removes any existing selection styling.
            $picker.find('.ui-datepicker-current-day')
                .removeClass("ui-datepicker-current-day").children().removeClass("ui-state-active");

            //This finds the selected link and styles it accordingly.
            //You can probably change the selectors, depending on your layout.
            $picker.find('a').each(function () {
                if ($(this).text() == inst.selectedDay) {
                    // Remove current selected date styles
                    $picker.find('.ui-datepicker-current-day')
                        .removeClass('ui-datepicker-current-day')
                        .children()
                        .removeClass("ui-state-active");
                    // td element of current date
                    var $selectedDate = $(this).parent();
                    // index of current date within all of the dates
                    var index = $allDates.index($selectedDate);
                    $allDates.slice(index, index + 5)
                        .addClass('ui-datepicker-current-day')
                        .find('a').addClass('ui-state-active');
                }
            });
            alert($date.datepicker('getDate')); // the clicked date
        }
    });
});

【讨论】:

  • 谢谢,这正是我要找的 ;)。我把它放在 beforeShow 中,这样我就可以在刷新后看到选定的日期。谢谢
猜你喜欢
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2012-09-11
  • 2021-07-24
  • 2010-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多