【问题标题】:Datepicker - How to set default date to next available date and not "today"?Datepicker - 如何将默认日期设置为下一个可用日期而不是“今天”?
【发布时间】:2012-03-15 12:46:03
【问题描述】:

我正在为我的网站使用 datepicker。我选择了几天不接受订单。当我禁用了一天并且实际上是那一天时,默认日期被选为那一天,即使我将其设置为不允许。

让我解释一下:

  • 3 月 17 日 - 禁用日期,因此用户无法选择该日期
  • 用户于 3 月 17 日访问该站点,文本字段显示默认日期为 3 月 17 日
  • 当用户提交表单而不选择新日期时 - 3 月 17 日将作为默认日期传递。

我希望将默认日期设置为下一个可用日期,在我的示例中为 3 月 18 日..

有没有办法将默认日期设置为禁用日期之后的下一个可用日期?

提前致谢。

到目前为止我的JS代码如下:

<script type="text/javascript">
jQuery.noConflict();
  jQuery(document).ready(function($) {

$("#input_1_16").datepicker({ beforeShowDay: nationalDays, minDate: 0, maxDate: "+4m"})   

natDays = [
    [1, 26], 
    [2, 6],
    [3, 15,17],
    [4, 27], 
    [5, 15,25], 
    [6, 6],
    [7,19], 
    [8,27], 
    [9,],
    [10,], 
    [11,], 
    [12,23,24,25,30,31]
];

function nationalDays(date) {
    for (i = 0; i < natDays.length; i++) {
      if (date.getMonth() == natDays[i][0] - 1
          && date.getDate() == natDays[i][1]) {
        return [false, natDays[i][2] + '_day'];
      }
    }
  return [true, ''];
}

  });
</script>

【问题讨论】:

    标签: jquery date datepicker


    【解决方案1】:

    我的朋友能够为我回答这个问题:代码如下,这里有一个工作示例http://jsfiddle.net/NHdEX/8/

    natDays = [
          [1,26], 
        [2,6],
        [3,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],
          [4,27], 
          [5,15,25], 
          [6,6],
          [7,19], 
          [8,27], 
          [9],
          [10], 
          [11], 
          [12,23,24,25,30,31]
    ];
    
    function getMinDate() {
        var now = new Date();
    
        var monthnumber = now.getMonth();
        var monthday = now.getDate();
        var year = now.getFullYear();
    
        //check if today is a holiday. 
        //by default do not skip any dates, allow user to select today. 
        var dayOffset = 0; 
    
        var date; 
        var currentDay = monthday; 
    
        var currentMonth = monthnumber; 
        for(var i = 0; i < natDays.length; i++){
            date = natDays[i]; 
            //check month
            if(date[0] == currentMonth+1){
                for(var j=1;j<date.length;j++){
                    currentDay == 0; 
                    while(date[j] == currentDay){
                        dayOffset++; 
                        currentDay++;
                    }
                }
            }
        }
    
    
        //calculate the new date. 
        now.setDate(now.getDate() + dayOffset);
    
        return (now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear();
    }
    
    function nationalDays(date) {
        for (i = 0; i < natDays.length; i++) {
          if (date.getMonth() == natDays[i][0] - 1
              && date.getDate() == natDays[i][1]) {
            return [false, natDays[i][2] + '_day'];
          }
        }
      return [true, ''];
    }
    
    
    $("#input_1_16").val(getMinDate());
    $("#input_1_16").datepicker({ beforeShowDay: nationalDays, minDate: getMinDate(), maxDate: "+4m"});  
    

    【讨论】:

    • 您确定这是解决方案吗?在 jsfiddle 上,我只能导航到 7 月,16 日之后没有任何选择
    • 在该示例中我将最大日期设置为 +4M,只需删除最大日期
    • 啊,太好了!甚至没有注意到。
    【解决方案2】:
    // Getting the first good day
    var default = new Date();
    while(nationalDays(default)[0]){
       default = new Date().setDate(default.getDate()+1);
    }
    
    // Creating by giving the first good day as default
    $("#input_1_16").datepicker({defaultDate: default beforeShowDay: nationalDays, minDate: 0, maxDate: "+4m"});
    

    【讨论】:

    • 您好,我尝试将您的代码插入我的,但似乎没有用。您是否建议我基本上复制您的代码并将其粘贴到我的行$("#input_1_16"..............."+4m"}) 上???如果你有完整的代码,包括我的,我应该使用它会很棒。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2011-06-22
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 2014-01-12
    • 2015-10-02
    相关资源
    最近更新 更多