调用日期选择器选择从另一个函数

问题描述:

内发挥作用,我有以下功能正在使用中承诺要恢复从异步Ajax调用数据:调用日期选择器选择从另一个函数

$("#mySelect").on('change', function() { 
     var mySelectValue = $('#mySelect').val(); 
     var promise = getAvailableDates(mySelectValue); 

     promise.done(function(data) { // tested and returning data 

     var array = data; 

     $('#a_date').datepicker({ // this function not working 
      dateFormat: "yy-mm-dd", 
      beforeShowDay: function (date) {  
       var string = jQuery.datepicker.formatDate('yy-mm-dd', date); 
       return [array.indexOf(string) >= 0 ] 
      } 
     }); 

     }); 
    }); 

为什么$('#a_date').datepicker()功能不另一个函数里面工作?

它不响应我的dateFormatbeforeShowDay设置,该设置基于从我的Ajax调用返回的数据。

如何从返回的承诺中调整Datepicker设置?

+0

你包括库? – Valkyrie

+0

@Valkyriee你的意思是CSS和JS?是的,日期选择器自己正在工作,但选项功能不在返回的承诺内工作。除此之外,它运行良好。 – JanisOzolins

+0

你可以发布'array'变量的值吗?其余的代码似乎是正确的。一个简单的例子是[这里](http://codepen.io/farooqkhan/pen/RVwrqe) –

你必须destroy重新初始化,通过下面的语句之前创建的第一个datetimepicker对象:

$("#datepicker").datepicker("destroy"); 

看到如下:

$(function() { 
 
    $("#datepicker").datepicker({ 
 
     dateFormat: "yy-mm-dd" 
 
    }); 
 
    
 
    $("#btn1").click(function(){ 
 
     $("#datepicker").datepicker("destroy"); 
 
     $("#datepicker").datepicker({ 
 
     dateFormat: "dd/mm/yy" 
 
     }); 
 
     console.log("Format changed, try to select a new date"); 
 
    }); 
 
    });
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> 
 
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
<p>Date: <input type="text" id="datepicker"></p> 
 
<input type="button" id="btn1" value="Change format">

+0

谢谢,这个工作就像一个魅力!作为JavaScript noob,这些我还不知道的小事情。 – JanisOzolins

+0

非常欢迎。我很高兴它帮助你。 – Alessandro