【问题标题】:drupal 7 date_popup module (update minDate onSelect event)drupal 7 date_popup 模块(更新 minDate onSelect 事件)
【发布时间】:2014-08-06 13:22:30
【问题描述】:

我正在使用 drupal 7 模块日期 (date_popup)。我有 2 个日期选择器。我需要通过单击第一个来更新“onSelect”事件的第二个日期选择器上的 minDate。它有效,但仅适用于第二次点击。如果我单击第一个日期选择器并选择日期,则不会发生任何事情并且 minDate 不会更新。当我第二次选择它时效果很好。为什么我的第一次点击不起作用?

这是我的模块的 datepicker init php 代码。

    <?php
    $form['leave'] = array(
            '#type' => 'date_popup', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
            '#title' => t('Leave'),
            '#default_value' => date('Y-m-d'),
            '#date_format' => $format,
            '#required' => TRUE, // Added
            '#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
            '#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
            '#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
            '#datepicker_options' => array(
                'changeMonth' => false,
                'changeYear' => false,
                'altField' => '#edit-leave-alt',
                'showOtherMonths' =>true,
                'selectOtherMonths' =>false,
                'altFormat' => 'D, M d, yy',
                'minDate' => 0,
            ), // Optional, as of 7.x-2.6+, used to pass in additional parameters from the jQuery Datepicker widget.
            '#attributes' => array('readonly' => 'readonly')

        );

$form['leave'] = array(
        '#type' => 'date_popup', // types 'date_popup', 'date_text' and 'date_timezone' are also supported. See .inc file.
        '#title' => t('Leave'),
        '#default_value' => date('Y-m-d'),
        '#date_format' => $format,
        '#required' => TRUE, // Added
        '#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
        '#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
        '#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
        '#datepicker_options' => array(
            'changeMonth' => false,
            'changeYear' => false,
            'altField' => '#edit-leave-alt',
            'showOtherMonths' =>true,
            'selectOtherMonths' =>false,
            'altFormat' => 'D, M d, yy',
            'minDate' => 0,
        ), // Optional, as of 7.x-2.6+, used to pass in additional parameters from the jQuery Datepicker widget.
        '#attributes' => array('readonly' => 'readonly')

    );
    ?>

这是我的js代码

(function($)  {
    Drupal.behaviors.ifly_search = {
        attach: function (context, settings) {
            $("#edit-leave-datepicker-popup-0").datepicker({
                onSelect: function(date,param) {
                    var data = new Date(param.currentYear, param.currentMonth, param.currentDay);
                    $("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
                },
            });
        }
    };
})(jQuery);

【问题讨论】:

标签: date drupal-7 onselect mindate


【解决方案1】:

只是为了充实我的评论。试试这个:

(function($)  {
        Drupal.behaviors.ifly_search = {
        attach: function (context, settings) {
          $("#edit-return-datepicker-popup-0").datepicker({minDate: new Date()});
          $("#edit-leave-datepicker-popup-0").datepicker({
            onSelect: function(date,param) {
                var data = new Date(param.selectedYear, param.selectedMonth, param.selectedDay);
                $("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
            },
          });
        }
    };
})(jQuery);

【讨论】:

  • 在我看来,这只是日期选择器的一个错误。当然,.datepicker("option", "minDate", data) 也应该可以使用
  • 好的,我找到了一个副本。我会将此标记为重复。基本问题是为 datepicker 设置选项 minDate
  • 此代码从第一次开始工作,但只有一次,如果我再次点击没有任何反应
  • 呵呵,是的,你是对的。你知道如果我们在另一行之后添加第二行旧格式,即$("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data);,它总是有效的。如果你问我,一些日期选择器错误。
  • 好的,改变了答案。事实证明,需要的是两种方法。一个初始化return-datepicker元素的minDate,然后我们使用datepicker("option")。使用什么来初始化 minDate 的问题。我使用了当前日期。但当然也可以像我在上面的评论中描述的那样做。
【解决方案2】:

Skarist,非常感谢! 这是工作代码:

Drupal.behaviors.ifly_search = {
        attach: function (context, settings) {
            $("#edit-leave-datepicker-popup-0").datepicker({
                onSelect: function(date,param) {
                    var data = new Date(param.selectedYear, param.selectedMonth, param.selectedDay);
                    $("#edit-return-datepicker-popup-0").datepicker({minDate: data}).val(date);
                    $("#edit-return-datepicker-popup-0").datepicker("option", "minDate", data).val(date);
                },
            });
        }
    };

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多