简体   繁体   English

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

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

I've seen solutions using the onBeforeShowDay but that is not what I need. 我已经看到了使用onBeforeShowDay解决方案,但这不是我所需要的。 What I need is when changing the either Month or Year I need to get a list of dates via AJAX and then use that list to disable the days in the current month. 我需要的是更改月份或年份时,我需要通过AJAX获取日期列表,然后使用该列表禁用当前月份中的日期。

Example

$('#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'];
        }
    });
});

EDIT 编辑
Thanks for the solution. 感谢您的解决方案。 To be specific, I was using the dynamic approach by adding the callback dynamically. 具体来说,我通过动态添加回调来使用动态方法。 Also it is important that AJAX call need to have async: false in order to get the correct data set on the array. 同样重要的是,AJAX调用需要具有async: false才能在阵列上获取正确的数据集。

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

So I just followed the answer and added: 所以我只是按照答案并添加了:

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

Again, much thanks! 再次非常感谢!

you can still use the onBeforeShowDay , since it will get called before the datepicker is displayed, because changing months will make the datepicker to render again. 您仍然可以使用onBeforeShowDay ,因为它将在显示日期选择器之前被调用,因为更改月份将使日期选择器再次呈现。

You can use an array that stores the list of dates and change this based on the result from your ajax call. 您可以使用存储日期列表的数组,并根据ajax调用的结果更改日期列表。 eg 例如

//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 ]
   }
});

here is the working fiddle 这是工作的小提琴

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

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