繁体   English   中英

如何在onChangeMonthYear之后禁用jQuery UI Datepicker中的日期

[英]How to disable dates in jQuery UI Datepicker after onChangeMonthYear

我已经看到了使用onBeforeShowDay解决方案,但这不是我所需要的。 我需要的是更改月份或年份时,我需要通过AJAX获取日期列表,然后使用该列表禁用当前月份中的日期。

$('#date_input').datepicker( "option", "onChangeMonthYear", function(year,month,inst) {
    // Perform AJAX call and get the list
    $.ajax({
        url: ajaxUrl,
        type: "post",
        data: serializedData,
        async: false,
        success: function (data) {
            // Here is where I want to update the display
            // and use the returned data as the basis for the disabled list
            // of the current month.
            // Let's say:
            // data = ['2015-10-15','2015-10-25','2015-10-13'];
        }
    });
});

编辑
感谢您的解决方案。 具体来说,我通过动态添加回调来使用动态方法。 同样重要的是,AJAX调用需要具有async: false才能在阵列上获取正确的数据集。

$('#date_input').datepicker( "option", "onChangeMonthYear", function(year,month,inst) {
    // The ajax call.    
});

所以我只是按照答案并添加了:

$('#date_input').datepicker( "option", "beforeShowDay", function(date) {
    // Update the list.    
});

再次非常感谢!

您仍然可以使用onBeforeShowDay ,因为它将在显示日期选择器之前被调用,因为更改月份将使日期选择器再次呈现。

您可以使用存储日期列表的数组,并根据ajax调用的结果更改日期列表。 例如

//at first only september dates will be disabled.
var array = ["2015-09-23","2015-09-24","2013-09-16"];

$('input').datepicker({
    onChangeMonthYear: function(year, month, inst) {
    // Perform AJAX call and get the list
    //override the array, and now the october dates will be disabled.
        $.ajax({
          url: ajaxUrl,
          type: "post",
          data: serializedData,
          async: false,
          success: function (data) {
              array = data; //["2015-10-23","2015-10-24","2013-10-16"];
           }
        });  
   },
   beforeShowDay: function (date) {
     var string = jQuery.datepicker.formatDate('yyyy-mm-dd', date);
     return [array.indexOf(string) == -1 ]
   }
});

这是工作的小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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