【问题标题】:How to disable dates in jQuery UI Datepicker after onChangeMonthYear如何在 onChangeMonthYear 之后在 jQuery UI Datepicker 中禁用日期
【发布时间】:2015-09-23 17:52:18
【问题描述】:

我见过使用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.    
});

再次感谢!

【问题讨论】:

  • 没问题,很高兴你修好了!

标签: javascript jquery datepicker jquery-ui-datepicker


【解决方案1】:

您仍然可以使用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 ]
   }
});

这里是工作的fiddle

【讨论】:

  • 你就是男人!多谢!我认为一旦日历已经显示,beforeShowday 就不会再次被触发。
猜你喜欢
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2012-06-22
  • 2011-04-29
  • 1970-01-01
  • 2021-06-08
相关资源
最近更新 更多