Help! How to make days enabled in UI datepicker after month change?

浪尽此生 提交于 2019-12-02 07:44:10

The problem is the date format here, when you're storing the dates they're coming out as 2010-1001 instead of 2010-10-01, so change this:

$daysWithRecords.push(dateToFind+$(this).text());

To this:

$daysWithRecords.push(dateToFind+"-"+$(this).text());

You can see it working here.


Here's an overall more optimized version as well, less looping and no infinitely growing array:

var daysWithRecords = [];

function initDaysArray( $xml , year , month ) {
  var d = year+'-'+month;
  daysWithRecords = 
    $xml.find('user[id="126"] whDateList[month="'+d+'"] date').map(function() {
      return d+"-"+$(this).text();
    }).get();
}

function checkAvailability(availableDays) {
    var checkdate = $.datepicker.formatDate('yy-mm-dd', availableDays);
    for(var i = 0; i < daysWithRecords.length; i++) { 
       if(daysWithRecords[i] == checkdate){
           return [true, "available"];
        }
    }
    return [false, ""];
}

$('#div').datepicker({ 
  dateFormat: 'yy-mm-dd',
  defaultDate: '2010-09-01', 
  onChangeMonthYear: getDays, 
  beforeShowDay: checkAvailability
});

You can test it here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!