2013-06-27 48 views
1

我的代碼非常適合作爲小提琴獨立使用,但一旦將代碼導入到我的WordPress環境中後,該代碼就無法正常工作。基本上我試圖限制基於日期範圍數組的可選日期。jQuery Datepicker未在WordPress環境中顯示

工作小提琴:http://jsfiddle.net/rLnTQ/658/

任何理由爲什麼當我導入我的WordPress網頁這是行不通的?我相信這可能是一些做的參考「beforeShowDay:disabledays」 ..

這裏是我的代碼嗎:

var unavailableDates = ["29-6-2013"]; 

function disabledays(date) { 
    dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear(); 
    if ($.inArray(dmy, unavailableDates) == 0) 
    { 
     return [false, "", "Unavailable"] 
    } 
    else 
    { 
     var day = date.getDay(); 
     return [true, "", ""]; 
    } 
} 

jQuery(document).ready(function($){ 
$('#bookingDate').datepicker({ 
           changeMonth:true, 
           changeYear:true, 
           dateFormat:"dd/mm/yy", 
           minDate:"+2d", 
           constrainInput: true, 
           beforeShowDay: disabledays 
          }); 
}); 
+0

什麼錯誤? –

+0

TypeError:$是不確定的if($ .inArray(dmy,unavailableDates)== 0){ – nematoth

+0

究竟是什麼問題?日期選取器沒有出現?你能發佈一個鏈接到你的網站或創建一個失敗的例子嗎? – Thomas

回答

1

你的代碼工作正常,好像你有$ alias conflict,有有幾個解決方案,爲它

1)jQuery.inArray代替$.inArray

2),我寧願是用的jQuery給noConflict函數來解決這個問題的另一種解決方法,你可以閱讀更多這裏http://api.jquery.com/jQuery.noConflict/

jQuery.noConflict(); 
(function($) { 
    $(function() { 
    // more code using $ as alias to jQuery 
    }); 
})(jQuery); 
// other code using $ as an alias to the other library 

你也可以試試這個代碼(代碼:)整齊版),

var unavailableDates = ["6-29-2013","6-27-2013","7-2-2013"]; 

function disabledays(date) { 
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();  

    for (i = 0; i < unavailableDates.length; i++) { 
     if(jQuery.inArray((m+1) + '-' + d + '-' + y,unavailableDates) != -1 || new Date() > date) { 
      return [false]; 
     } 
     return [true]; 
    } 
} 

    jQuery(document).ready(function($){ 
    $('#bookingDate').datepicker({ 
            changeMonth:true, 
            changeYear:true, 
            dateFormat:"dd/mm/yy", 
            minDate:"+2d", 
            constrainInput: true, 
            beforeShowDay: disabledays 
           }); 
    }); 

這裏是的jsfiddle http://jsfiddle.net/rLnTQ/659/

+0

這沒有任何結果。同樣的錯誤.. – nematoth

+0

它被調用。我把它放置如下: dmy = date.getDate()+「 - 」+(date.getMonth()+ 1)+「 - 」+ date.getFullYear(); alert('disabledays called'); if($ .inArray(dmy,unavailableDates)== 0) – nematoth

+0

我已根據您的要求更新我的答案代碼,請嘗試一下。它工作完美的小提琴 – wakqasahmed