【问题标题】:jQuery datepicker restrict monthsjQuery datepicker 限制月份
【发布时间】:2012-05-30 07:07:46
【问题描述】:

对不起,伙计们,似乎在手册中找不到这个,但我想它一定在那里。我需要将 UI 日期选择器上显示的月份限制为仅 5 月、6 月、7 月、8 月、9 月和 10 月。我看到很多 minDate 和 maxDate 但他们似乎为 minDate 设置日期范围 todate maxDate +3M 等请提出建议。

【问题讨论】:

标签: jquery jquery-ui datepicker


【解决方案1】:

OnChangeMonthYear 是您要附加到的事件。您可能需要检查该月份是否超出了允许的月份范围,然后将日期重置为下一年的下一个允许月份的第一天。

$('.selector').datepicker({
    onChangeMonthYear: function(year, month, inst) {
    //0 based index on the months, from what I remember.
        if(month<4){
            year = year-1;
             $( this ).datepicker( "setDate" , new Date(year, 9, 1) )
             $( this ).datepicker("refresh");
        }

        if(month>9){
            year = year+1;
             $( this ).datepicker( "setDate" , new Date(year, 4, 1) )
             $( this ).datepicker("refresh");
        }

    }
});

【讨论】:

  • 杰森,不是 guite 仍然需要点击没有显示的“非”月份,所以你去 10 月,点击点击等,直到 5 月的计数器达到 5
  • 嗯,您可能还需要调用刷新函数。让我看看。
  • 杰森·戈蒂特! - 而不是使用 inst.datepicker 更改为 jQuery(this).datepicker(blah blah)
  • Jason - 你想为其他人编辑你的答案吗?或者我应该
  • @Ghokun 如果用户手动输入日期,您可能需要对文本字段进行一些 jquery 验证。或者可能附加到日期选择器上的关闭事件,如果日期超出您的范围,则重置日期。
【解决方案2】:
var currentYear = (new Date).getFullYear();

$( ".selector" ).datepicker(
  { minDate: new Date(currentYear, month, day, hours, minutes, seconds, milliseconds), 
    maxDate: new Date(currentYear, month, day, hours, minutes, seconds, milliseconds) });

这应该可以,不确定是否有效。但给出了主意

【讨论】:

  • Ghokun - 差不多,但我不想设定年份,即任何一年的五月,而不仅仅是设定年份的五月
  • Ghokun 非常非常近,但是当我到今年 10 月时,我如何将他们移至明年 5 月?
  • 我猜 Jason 刚刚发布了我即将发布的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多