在jquery datepicker中禁用动态日期和天数

时间:2013-11-21 06:31:11

标签: javascript jquery datepicker

我已经使用了jquery ui datepicker,我想禁用一些来自数据库和日期的特定日期范围。所以我写了下面的代码,但是它不能正常工作

function checkAvailability(mydate){
var $return=true;
var $returnclass ="available";
$checkdate = $.datepicker.formatDate('dd-mm-yy', mydate);
    var datas  =$.parseJSON(disabledDays);
            var unavailableDates=[];
            for(i in datas){
                unavailableDates.push(datas[i]); 
            }

 for(var i = 0; i < unavailableDates.length; i++) 
    {    
       if(unavailableDates[i] == $checkdate)
       {
            $return = false;
            $returnclass= "unavailable";
       }
    } 
 var day = mydate.getDay();         
 //return [(day!=0) && $return,$returnclass];

        var disabledDaysRange  =$.parseJSON(disabledDays_Range);
        var unavailableDays=[];
        for(j in disabledDaysRange){
                unavailableDays.push(disabledDaysRange[j]); 
         }
         var days_return = '';
        for(var j = 0; j < unavailableDays.length; j++) 
         {    
                if(unavailableDays[j] == day)
                    {
                        days_return =days_return.concat( day +'!= '+unavailableDays[j]+ ' && '); 
                    }
          } 
              if(days_return!=''){
                    days_return = "( " + days_return + ")"; 
                    days_return = days_return.replace("&& ) ", " )");
               }
           if(days_return!=''){         
            return [days_return && ($return),$returnclass];
           }else{
               return [$return,$returnclass];
           }
}     

1 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/devmgs/E9m8G/

进行演示
$(function() {
    $( "#datepicker" ).datepicker({
    minDate: 2,
    maxDate: "+3M",
    dateFormat: "DD, d MM, yy",
    beforeShowDay: function(day) {
        var newday = day.getDay();
        if (newday == 1 || newday == 2 || newday == 3 || newday == 4) {
            return [false, ""]
        }
        else {
            var disabledDays = ["10-25-2013"];        
        var m = day.getMonth(), d = day.getDate(), y = day.getFullYear();
for (i = 0; i < disabledDays.length; i++) {
    if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) {
        return [false];
    }
}
return [true];
    }
        }


});
  });