【问题标题】:jQuery UI datepicker maxdatejQuery UI 日期选择器 maxdate
【发布时间】:2011-09-13 01:08:13
【问题描述】:

我需要让我的 jQueryUI 日期选择器的 maxDate 根据日期动态更新。现在,正如您将在下面看到的,有一个用于禁用周一至周四的功能,因为我们的团队只能在周五至周日报告问题。所以我需要做的是让团队可以在周末继续报告,但下周末直到周四才能继续报告。例如,今天是周一,团队仍然可以报告上周四至周日的问题,但直到周四才能选择下周末的日期。这可能吗?

这是我目前的代码:

  var fullDate = new Date();
  var twoDigitMonth = ((fullDate.getMonth().length+1) === 1)? (fullDate.getMonth()+1) : '0' + (fullDate.getMonth()+1);      

  $('#startdate').datepicker({
    maxDate: twoDigitMonth + "/" + fullDate.getDate() + "/" + fullDate.getFullYear(),
    beforeShowDay: disableSpecificWeekDays
  });


  function disableSpecificWeekDays(date) {
    if((date.getDay()==0) || (date.getDay() == 5) || (date.getDay() == 6) ){
        return [true];
    }else{
        return [false];
    }
  }

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-datepicker


    【解决方案1】:

    这样的东西应该适合你:

    function getMaxDate() {
        // Maximum date is today by default.
        var maxDate = new Date();
    
        // Is today Thursday or greater (Thurs. - Sat.)
        if (maxDate.getDay() >= 4) {
            // Yes? Make the date today's date plus the difference between today and
            // next Sunday
            maxDate.setDate(
                maxDate.getDate() + (7 - maxDate.getDay()));
        }
        return maxDate;
    }
    
    $('#startdate').datepicker({
        maxDate: getMaxDate(),
        beforeShowDay: disableSpecificWeekDays
    });
    

    示例: http://jsfiddle.net/RxQB4/

    基本上,编写一个封装您的逻辑的函数。初始化日期选择器时,调用该函数来检索最大日期。

    【讨论】:

      猜你喜欢
      • 2020-03-26
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      相关资源
      最近更新 更多