2012-03-15 86 views
2

我正在使用datepicker爲我的網站。我選擇了幾天不接受訂單。當我禁用了一天,並且實際上是那一天時,即使我將它作爲不允許的日期,也會選擇默認日期作爲那一天。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> 

回答

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"}); 

+0

你確定這是解決方案嗎?在jsfiddle上,我只能導航到7月,並且沒有什麼可以選擇過去的第16個 – MetalFrog 2012-03-16 13:11:44

+1

我在該示例中將最大日期設置爲+ 4M,只需刪除最大日期 – Redwall 2012-03-16 16:16:35

+0

啊,真好!甚至沒有注意到這一點。 – MetalFrog 2012-03-16 16:17:25

0
// 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"}); 
+0

嗨,我試圖插入你的代碼在我的,但似乎沒有工作。你是否建議我基本上覆制你的代碼並將其粘貼到我的行'$(「#input_1_16」...............「+ 4m」})'???如果你有完整的代碼,包括我的應該使用那將是很棒的。謝謝 – Redwall 2012-03-15 17:19:12

相關問題