简体   繁体   English

jQuery Datepicker:用户下个月或上个月?

[英]jQuery Datepicker: User Going to Next Month or Previous Month?

I need to repopulate the datepicker with custom css after the user either clicks the 'Next' month or the 'Previous' month. 在用户点击“下一个”月份或“上个月”之后,我需要使用自定义css重新填充日期选择器。 I'm tapping into the onChangeMonthYear event but don't know which way the user is going (next month or previous month). 我正在进入onChangeMonthYear事件,但不知道用户的走向(下个月或上个月)。 Is there anything simple built in that would give me this? 有什么简单的内置会给我这个吗? If not, what is the simplest solution? 如果没有,最简单的解决方案是什么?

* Adding code here to clarify Please note that the events will actually come in via server-side and I've just dummied out some data here while I build* *在此处添加代码以澄清请注意事件实际上将通过服务器端进入,而我在构建时只是在这里贬低了一些数据*

 var Event = function(text, className) {
            this.text = text;
            this.className = className;
        };

        var events = {};
        events[new Date("11/12/2012")] = new Event("A Good Day", "yellowDot");
        events[new Date("11/20/2012")] = new Event("Mkay", "blueDot");

        $('#date').datepicker({              
           beforeShowDay: function (date) {
               var event = events[date];
               if(event) {
                   return [true, event.className, event.text];
               }
               else {
                   return [true, '', ''];
               }
           },
           onChangeMonthYear: function () {
               //reload another calendar, past or future?

           }
        });

You're passing an onChangeMonthYear handler function to the datepicker widget. 您正在将onChangeMonthYear处理函数传递给datepicker小部件。 That function will be called with three arguments: 该函数将使用三个参数调用:

  • the newly selected year, 新选择的一年,
  • the newly selected month, 新选的月份,
  • the datepicker widget instance. datepicker小部件实例。

Therefore, you can write something like: 因此,你可以这样写:

onChangeMonthYear: function(year, month, widget) {
    reloadCalendar(month, year);
}

The widget instance argument is provided as a shortcut to avoid going through the bridge if you want to manipulate the autocomplete widget. 如果要操作自动完成窗口小部件,则窗口小部件实例参数作为快捷方式提供,以避免通过桥。 For example, should you want to close the datepicker immediately, it allows you to write: 例如,如果要立即关闭datepicker,它允许您编写:

widget.hide();

Instead of: 代替:

$(this).datepicker("hide");

Datepicker created dynamically, so you should use .live Datepicker动态创建,因此您应该使用.live

$('.ui-datepicker-next').live("click", function() {
  alert(1)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM