2012-03-19 157 views
2

我正在編寫一個Struts Web應用程序,在我的一個jsps上使用JQuery 1.7 Datepicker來實現可突出顯示用戶提醒的日曆。JQuery:Datepicker突出顯示日期範圍

我有一個問題:

我想強調一個範圍在日期選擇日期的。隨着我的代碼後,這個Javascript控制檯顯示沒有錯誤,但是當我登錄的日期範圍是不突出,這是我的函數:

$(function(){ 

      $('#datepicker').datepicker({ 
       flat: true, 
       numberOfMonths: [1,1], 
       dateFormat: 'dd/mm/yy', 
       beforeShowDay: highlightDays 
      }); 

我有提醒的數組,每個提醒有3個屬性,的startDate,結束日期和單位(相關單元)

在beforeShowDay事件,功能highlightDays被激活:

function highlightDays(date) { 

    //For all reminders in the db 
    for (var i = 0; i < reminders.length; i++) { 

    //If the current date in between the start and end date of reminder 
     if((new Date(reminders[i].start).getTime()) 
         <= date.getTime()        
         && (date.getTime())       
         <= (new Date(reminders[i].end).getTime())) date 
       { 
        //Then highlight the current date    
        return [true, 'ui-state-highlight',reminders[i].unit]; 

       }else{ //Otherwise do not highlight       
        return [true, '']; 
       } 
      }   
     } 

你有什麼想法,有什麼我可以做錯了什麼?到目前爲止我所實施的內容對我來說很有意義,所以我不確定會出現什麼問題。我真的很感激一些指導!

非常感謝您的閱讀!

+1

您是否在選擇範圍或使用該值時遇到問題? – 2012-03-19 19:50:57

+0

哦謝謝評論!那麼我的問題是,即使我有一個範圍(提醒的開始和結束日期),我試圖突出日曆中的日子,它只會突出顯示當天,而不是我問過的日期範圍它突出在highlightDays功能 – AndresR 2012-03-19 19:57:28

+1

hiya,你在找這樣的東西:http://jsbin.com/evudo,乾杯 – 2012-03-19 19:59:34

回答

0

我已經成功完成了datepicker的日期和日期選擇器,因此我將其修改爲一個datepicker。以下是代碼:

$(function() { 
      var today = new Date(); 
      var thisYear = (today).getFullYear(); 
      var fromDate = '1/1/2000' //this is the initial from date to set the datepicker range 
      var toDate = '1/7/2000' // this is the initial to date to set the datepicker range 

//... initialize datepicker.... 
    }, 
    beforeShowDay: function(date){ 
     //if the date is in range 
     if (date >= fromDate && date <= toDate) { 
      return [true, 'ui-individual-date', '']; //applies a css class to the range 
     } 
     else { 
      return [true, '', '']; 
      } 
    }, 
    onSelect: function (dateText, obj) { 

//sets the new range to be loaded on refresh call, assumes last click is the toDate    
    fromDate = toDate; 
    toDate = new Date(dateText); 

    $(".classDp1").datepicker("refresh"); 
    $(".classDp2").datepicker("refresh"); 
    }, 

每次刷新beforeShowDay函數時都會調用新的fromDate和toDate範圍。讓變量超出函數的範圍並對其進行修改可以使CSS中的高亮顯示適用於每次點擊。

相關問題