2011-09-13 131 views
2

我需要爲我的jQueryUI日期選擇器的maxDate根據當天動態更新。現在,正如您將在下面看到的,由於我們的團隊只能在星期五至星期日報告問題,因此有一項功能用於禁用星期一至星期四。所以我需要做的就是讓球隊能夠在週末繼續報道,但直到週四才能在下週末繼續報道。例如,今天是星期一,球隊仍然可以報告上週四至週日的問題,直到週四才能選擇下週末的一天。這可能嗎?jQuery UI datepicker 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]; 
    } 
    } 

回答

2

像這樣的東西應該爲你工作:

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/

基本上,編寫一個函數,封裝你的邏輯。初始化日期選擇器時,調用該函數以檢索最大日期。