jquery datepicker onChangeMonthYear [英] jquery datepicker onChangeMonthYear

查看:420
本文介绍了jquery datepicker onChangeMonthYear的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



单击上一个或下一个,日历应该: -


  1. 相应地前往/下个月。

  2. 将当月的第一天设为当前所选日期。

  3. 提醒该日期

问题出在#2。



我正在使用 setDate 做到这一点,它是以无限递归结束的。因为,我在onChangeMonthYear上调用setDate。而且setDate也在内部开始onChangeMonthYear。



如果点击上一个/下一个,我该如何实现这3个。

解决方案

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

  var changingDate = false; 
$('。selector')。datepicker({
onChangeMonthYear:function(year,month,inst){
if(changingDate){
return;
}
changingDate = true;
//你的setDate()调用这里
changingDate = false;
}
});


I am using the jquery ui datepicker in my app. I have created an inline datepicker. I am having a problem with onChangeMonthYear. I have simplified the example to bare minimum.

Onclick of "prev" or "next", the calendar should: -

  1. Go to the previous/next month accordingly.
  2. Set the first day of that month as the current selected date.
  3. alert that date

The problem is with #2.

I am using setDate to do that, and it is ending up in infinite recursion. Because, I am calling setDate inside onChangeMonthYear. And setDate is also firing onChangeMonthYear internally.

How can I achieve those 3 things when prev/next is clicked.

解决方案

Try setting a flag that you can query in your event handler to prevent the infinite loop:

var changingDate = false;
$('.selector').datepicker({
    onChangeMonthYear: function(year, month, inst) {
        if (changingDate) {
            return;
        }
        changingDate = true;
        // your setDate() call here
        changingDate = false;
    }
});

这篇关于jquery datepicker onChangeMonthYear的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆