简体   繁体   中英

BeforeShowDay doesn't changed in datepicker

I have a function to disabled specific days before show date in datepicker , but I have an error: when I call my function in first time it works, but in second call I still have the first result. For example on the first call I have Sunday disabled, when I call the function again I have Sunday disabled too. I see that the error is in the variable used in BeforShowDay , is still not changed just inside datepicker , but outside is changer in each call.

This is my code:

function loadFreeDays (etablissement) {
  var array=[];
  $.ajax({
    type: "POST",
    url: "/promo3/etablissments/getFreeDays/"+etablissement,
    data:  {'etablissement': etablissement } ,
    dataType: 'json',
    success: function(response){
      $.each(response, function(i, value) {
        array.push(value);
      }),  

      /*console.log(" diasbled days are "+array); here variable still is changed  */ 

      $('#datepicker').datepicker({
        beforeShowDay: function(date){
          /* here array variable still not changed */
          for (i = 0; i < array.length; i++) {

            if (date.getDay() == array[i][0]) {
              return [false];
            }
          }
          var string = jQuery.datepicker.formatDate('d-m-yy', date); 
          var day = date.getDay(); 
          return [true, '']; 
        }
      });

    },
    error: function(x, e) { }
  });
}

I solve the problem by colling $('#txtDate').datepicker("destroy"); before ajax function. It work now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM