jQuery UI Datepicker - Disable specific days

前端 未结 6 2108
予麋鹿
予麋鹿 2020-11-29 03:20

Is there any (easy) way to set the jQuery UI Datepicker to disallow selection of specific, predetermined days?

I was able to get this approach working, however, it p

相关标签:
6条回答
  • 2020-11-29 03:24
     var unavailableDates = ["19-8-2014","14-9-2014"];
    
     function unavailable(date) {
        dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" +date.getFullYear();
        if ($.inArray(dmy, unavailableDates) < 0) {
         return [true,"","Book Now"];
        } else {
      return [false,"","Booked Out"];
      }
     }
    
     $('#iDate').datepicker({ beforeShowDay: unavailable });
    

    Live Demo: http://jsfiddle.net/vfod0krm/

    0 讨论(0)
  • 2020-11-29 03:31
    beforeShowDay: $.datepicker.noWeekends,
    

    will be

    beforeShowDay: noWeekendsOrHolidays,
    
    0 讨论(0)
  • 2020-11-29 03:33

    The problem with IE is most probably on the following line:

    altField: '#alternate',
    }); 
    

    Try to remove the comma symbol and it should work.

    0 讨论(0)
  • 2020-11-29 03:39

    Here is a way to disable specific dates from being selected:

    var unavailableDates = ["9-5-2011","14-5-2011","15-5-2011"];
    
    function unavailable(date) {
      dmy = date.getDate() + "-" + (date.getMonth()+1) + "-" + date.getFullYear();
      if ($.inArray(dmy, unavailableDates) < 0) {
        return [true,"","Book Now"];
      } else {
        return [false,"","Booked Out"];
      }
    }
    
    $('#iDate').datepicker({ beforeShowDay: unavailable });
    

    Source: jQuery - Datepicker - Disable Specific Dates

    0 讨论(0)
  • 2020-11-29 03:44

    Have you tried declaring natDays properly with the 'var' keyword in front?

    Also - you have an extra comma at the end of your natDays definition.

    natDays[i][2] won't work as your the arrays only have 2 items in them - try just ''

    Additionally, you'll want to set your beforeShowDay function name correctly - doesn't look like it's even calling your custom function

    0 讨论(0)
  • 2020-11-29 03:47

    You can use the beforeShowDay option. I needed to disable any day past the 28th of the month. Here is my code.

    $('.datepicker').datepicker({
        dateFormat: "yy-mm-dd",
        beforeShowDay: function(date) {
            var day = date.getDate();
            if (day > 28) {
                return [false];
            } else {
                return [true];
            }
        }
    });
    

    Here is more information about it: http://api.jqueryui.com/datepicker/#option-beforeShowDay

    The date variable passed into the beforeShowDay callback is a JavaScript date object, so it can be formatted using various libraries, a timestamp can be retrieved using date.getTime(), etc.

    0 讨论(0)
提交回复
热议问题