7

我在我的应用程序中使用 jquery ui datepicker。我创建了一个内联日期选择器。我遇到了onChangeMonthYear的问题。我已将示例简化为最低限度。

点击“上一个”或“下一个”,日历应该: -

  1. 相应地转到上/下个月。
  2. 将该月的第一天设置为当前选定的日期。
  3. 提醒那个日期

问题在于#2。

我正在使用setDate来做到这一点,它最终以无限递归结束。因为,我在 onChangeMonthYear 中调用 setDate。setDate 也在内部触发 onChangeMonthYear。

单击上一个/下一个时,如何实现这三件事。

4

3 回答 3

8

尝试设置一个可以在事件处理程序中查询的标志,以防止无限循环:

var changingDate = false;
$('.selector').datepicker({
    onChangeMonthYear: function(year, month, inst) {
        if (changingDate) {
            return;
        }
        changingDate = true;
        // your setDate() call here
        changingDate = false;
    }
});
于 2009-07-07T13:21:29.997 回答
1

无限递归意味着您通知了错误的月份。
确保你从 1 减少month

于 2014-08-13T17:12:35.597 回答
0
onChangeMonthYear: function(year, month, inst){
    $(this).datepicker( "setDate", new Date(year, month-1, 1) );
}
于 2014-09-13T19:12:15.173 回答