向 jQuery DatePicker 添加回调的正确方法

发布于 2024-09-30 09:03:29 字数 929 浏览 4 评论 0原文

DatePicker 有一个内部函数 _adjustDate(),我想在其后添加一个回调。

当前方法


这就是我目前正在做的事情。基本上,只需将函数定义替换为函数本身,再加上新的操作。这会影响所有日期选择器,我也不确定是否需要。

$(function(){

   var old = $.datepicker._adjustdate;

   $.datepicker._adjustDate = function(){
      old.apply(this, arguments);

      // custom callback here     
      console.log('callback');
   };

});

_adjustDate 是在下个月/上个月点击期间调用的日期。该函数采用三个参数。我很好奇是否有更好的方法来添加回调。


预期结果


我希望最终结果如下所示;其中 afterAjustDate 是回调句柄:

$('#datepicker').datepicker({ 
       showButtonPanel: true
     , afterAdjustDate: function(){ $(this).find('foo').addClass('bar'); }
});

仅供参考,


onChangeMonthYear 不是可接受的事件替代方案,live()/delegate() 也不是(不适用于 IE)绑定选项。

这是因为需要在选择月份后应用类/操作元素。更改月份会重新创建日历中的元素;当执行此操作时,jQueryUI 不够智能,无法继承类更改。因此,更改日期后我需要回调。

DatePicker has an internal function, _adjustDate(), which I'd like to add a callback after.

Current Method


This is how I'm currently doing this. Basically, just replacing the function defintion with itself, plus the new operations. This effects all datepickers, which I'm not sure is desired, either.

$(function(){

   var old = $.datepicker._adjustdate;

   $.datepicker._adjustDate = function(){
      old.apply(this, arguments);

      // custom callback here     
      console.log('callback');
   };

});

_adjustDate is what's called during the next/prev month clicks. The function takes three parameters. I'm curious if there's a better way to add the callback.

Expected Result


I'd like to have the end result look like this; where afterAjustDate is the callback handle:

$('#datepicker').datepicker({ 
       showButtonPanel: true
     , afterAdjustDate: function(){ $(this).find('foo').addClass('bar'); }
});

FYI


onChangeMonthYear is not an acceptable event alternative, neither are the live()/delegate() (don't work in IE) binding options.

This comes from the need of applying classes/manipulating elements after the month is selected. Changing the month recreates elements in the calendar; when this is performed jQueryUI is not smart enough to inherit the class changes. Thus, I need a callback after changing the date.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

缱倦旧时光 2024-10-07 09:03:29

有一个 onSelect 选项,您可以使用它来将回调函数传递给日期选择器。听起来这可能是满足您需求的内置解决方案。

$('.datepicker').datepicker({
  onSelect: function(selectedDate) {
    // custom callback logic here
    alert(selectedDate);
  }
});

There is an onSelect option that you can use to pass a callback function to the datepicker. It sounds like this might be a built-in solution to what you need.

$('.datepicker').datepicker({
  onSelect: function(selectedDate) {
    // custom callback logic here
    alert(selectedDate);
  }
});
东京女 2024-10-07 09:03:29

我写了一个小演示来执行此操作...

我创建一个对象文字,其中包含 $.datepicker 的扩展,然后在 $.datepicker 和我的对象上执行 $.extend 。

您可以在这里查看: http://jsfiddle.net/NHN4g/4/

这是扩展本身:

(function($){
    var datepickerExtensions = {
        _oldAdjustDate: $.datepicker._adjustDate,
        _adjustDate: function(id, offset, period) { 
            var target = $(id);
            var inst = this._getInst(target[0]);
            var afterAdjustDate = this._get(inst, 'afterAdjustDate');
            this._oldAdjustDate(id, offset, period);
            if(afterAdjustDate && typeof afterAdjustDate === 'function'){
                afterAdjustDate(id, offset, period);
            }
        }
    }
    $.extend($.datepicker, datepickerExtensions);
})(jQuery);

和演示:

(html)

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all">
<div class="demo">
    <p>Date: <input type="text" id="datepicker"></p>
</div><!-- End demo -->

(javascript)

var changed = false;
$("#datepicker").datepicker({ 
    afterAdjustDate: function(i,o,p){
        if(!changed){
            $('.ui-datepicker-month').css('color', '#f00');
        }
        changed = !changed;
    }
});

I wrote a small demo that does this...

I create an object literal that contains the extensions to $.datepicker and then do $.extend on $.datepicker and my object.

You can check it out here: http://jsfiddle.net/NHN4g/4/

Here's the extension itself:

(function($){
    var datepickerExtensions = {
        _oldAdjustDate: $.datepicker._adjustDate,
        _adjustDate: function(id, offset, period) { 
            var target = $(id);
            var inst = this._getInst(target[0]);
            var afterAdjustDate = this._get(inst, 'afterAdjustDate');
            this._oldAdjustDate(id, offset, period);
            if(afterAdjustDate && typeof afterAdjustDate === 'function'){
                afterAdjustDate(id, offset, period);
            }
        }
    }
    $.extend($.datepicker, datepickerExtensions);
})(jQuery);

And the demo:

(html)

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/base/jquery-ui.css" type="text/css" media="all">
<div class="demo">
    <p>Date: <input type="text" id="datepicker"></p>
</div><!-- End demo -->

(javascript)

var changed = false;
$("#datepicker").datepicker({ 
    afterAdjustDate: function(i,o,p){
        if(!changed){
            $('.ui-datepicker-month').css('color', '#f00');
        }
        changed = !changed;
    }
});
柠北森屋 2024-10-07 09:03:29

在 Datepicker v1.0.0 上,您可以使用 pick 选项。

    var date = new Date();
    $('.datepicker-trigger').datepicker({
        format: 'mm/dd/yyyy',
        autoPick: false,
        startDate: date.getDate(),
        changeMonth: false,
        changeYear: false,
        autoHide: true,
        pick: function (selectedDate) {
            console.log(selectedDate);
        }
    });

On Datepicker v1.0.0 you can use the pick option.

    var date = new Date();
    $('.datepicker-trigger').datepicker({
        format: 'mm/dd/yyyy',
        autoPick: false,
        startDate: date.getDate(),
        changeMonth: false,
        changeYear: false,
        autoHide: true,
        pick: function (selectedDate) {
            console.log(selectedDate);
        }
    });
醉酒的小男人 2024-10-07 09:03:29

打开源文件,您会看到有几个回调函数可以在 default_options 中使用

        onSelectDate: function () {},
        onSelectTime: function () {},
        onChangeMonth: function () {},
        onGetWeekOfYear: function () {},
        onChangeYear: function () {},
        onChangeDateTime: function () {},
        onShow: function () {},
        onClose: function () {},
        onGenerate: function () {},

Open source file and you fill see there are several callback function you can use in default_options

        onSelectDate: function () {},
        onSelectTime: function () {},
        onChangeMonth: function () {},
        onGetWeekOfYear: function () {},
        onChangeYear: function () {},
        onChangeDateTime: function () {},
        onShow: function () {},
        onClose: function () {},
        onGenerate: function () {},
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文