【问题标题】:Jquery UI datepicker. Disable array of DatesJquery UI 日期选择器。禁用日期数组
【发布时间】:2018-05-28 16:26:14
【问题描述】:

我一直在尝试寻找我的 Jquery ui datepicker 问题的解决方案,但我没有运气。这就是我想要做的......

我有一个应用程序,我正在执行一些复杂的 PHP 来返回我希望从 Jquery UI Datepicker 中屏蔽的日期 JSON 数组。我正在返回这个数组:

["2013-03-14","2013-03-15","2013-03-16"]

有没有一种简单的方法可以简单地说:从日期选择器中阻止这些日期?

我已阅读 UI 文档,但没有发现任何对我有帮助的内容。有人有什么想法吗?

【问题讨论】:

标签: jquery-ui jquery-ui-datepicker


【解决方案1】:

您可以使用beforeShowDay 来执行此操作

以下示例禁用 2013 年 3 月 14 日至 2013 年 3 月 16 日的日期

var array = ["2013-03-14","2013-03-15","2013-03-16"]

$('input').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [ array.indexOf(string) == -1 ]
    }
});

演示:Fiddle

【讨论】:

  • 我有点明白你的打算。我将如何合并我的日期数组。
  • 哇。你真是个天才兄弟。我不知道为什么这个问题的堆栈溢出帖子中没有这个问题,因为它被问了一百万次。上面贴的所有例子都太复杂了,这个很好很简单。 5星。谢谢。
  • 我已经下载了代码jqueryui.com/download/…,但是当我将上述代码集成到其中时,它不起作用请帮助
  • 一个赞是不够的。有些人使事情变得比他们需要的更复杂。非常感谢您提供了一个简单、有效的 sn-p。非常有用。
  • 如果您还想排除这些自定义日期之外的周末,请将返回行替换为:return array.indexOf(string) != -1 ? [false] : $.datepicker.noWeekends(date);
【解决方案2】:

IE 8 没有 indexOf 功能,所以我用 jQuery inArray 代替。

$('input').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [$.inArray(string, array) == -1];
    }
});

【讨论】:

  • @ChristopherStrydom 你会惊讶于还有多少人还在使用它。
  • 这是因为 IE8 是最新的 IE 浏览器,可以在 WinXP (SP3) 上运行。
  • 它确实支持字符串上的 indexOf() 函数,用于查找该字符串中子字符串的位置。
【解决方案3】:

如果您还想阻止星期日(或其他日子)以及日期数组,我使用以下代码:

jQuery(function($){

    var disabledDays = [
       "27-4-2016", "25-12-2016", "26-12-2016",
       "4-4-2017", "5-4-2017", "6-4-2017", "6-4-2016", "7-4-2017", "8-4-2017", "9-4-2017"
    ];

   //replace these with the id's of your datepickers
   $("#id-of-first-datepicker,#id-of-second-datepicker").datepicker({
      beforeShowDay: function(date){
         var day = date.getDay();
         var string = jQuery.datepicker.formatDate('d-m-yy', date);
         var isDisabled = ($.inArray(string, disabledDays) != -1);

         //day != 0 disables all Sundays
         return [day != 0 && !isDisabled];
      }
   });
});   

【讨论】:

    【解决方案4】:
    $('input').datepicker({
       beforeShowDay: function(date){
           var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
           return [ array.indexOf(string) == -1 ]
       }
    });
    

    【讨论】:

      【解决方案5】:

      beforeShowDate 对我不起作用,所以我继续开发自己的解决方案:

      $('#embeded_calendar').datepicker({
                     minDate: date,
                      localToday:datePlusOne,
                     changeDate: true,
                     changeMonth: true,
                     changeYear: true,
                     yearRange: "-120:+1",
                     onSelect: function(selectedDateFormatted){
      
                           var selectedDate = $("#embeded_calendar").datepicker('getDate');
      
                          deactivateDates(selectedDate);
                         }
      
                 });
      
      
                    var excludedDates = [ "10-20-2017","10-21-2016", "11-21-2016"];
      
                    deactivateDates(new Date());
      
                  function deactivateDates(selectedDate){
                      setTimeout(function(){ 
                            var thisMonthExcludedDates = thisMonthDates(selectedDate);
                            thisMonthExcludedDates = getDaysfromDate(thisMonthExcludedDates);
                             var excludedTDs = page.find('td[data-handler="selectDay"]').filter(function(){
                                 return $.inArray( $(this).text(), thisMonthExcludedDates) >= 0
                             });
      
                             excludedTDs.unbind('click').addClass('ui-datepicker-unselectable');
                         }, 10);
                  }
      
                  function thisMonthDates(date){
                    return $.grep( excludedDates, function( n){
                      var dateParts = n.split("-");
                      return dateParts[0] == date.getMonth() + 1  && dateParts[2] == date.getYear() + 1900;
                  });
                  }
      
                  function getDaysfromDate(datesArray){
                        return  $.map( datesArray, function( n){
                          return n.split("-")[1]; 
                      });
                   }
      

      【讨论】:

      • 我们可以在 dateRangePicker 上做同样的事情吗??
      【解决方案6】:

      对于 DD-MM-YY,请使用此代码:

      var 数组 = ["03-03-2017', '03-10-2017', '03-25-2017"]

      $('#datepicker').datepicker({
          beforeShowDay: function(date){
          var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
          return [ array.indexOf(string) == -1 ]
          }
      });
      
      function highlightDays(date) {
          for (var i = 0; i < dates.length; i++) {
          if (new Date(dates[i]).toString() == date.toString()) {
              return [true, 'highlight'];
          }
      }
      return [true, ''];
      }
      

      【讨论】:

      • 是否可以使用 daterangepicker,我想禁用 date-range-picker 上的日期数组??
      • 你能定义你的问题吗?
      • 我有日期范围选择器假设:01/01/2017 - 31/03/2017。我想突出显示此范围之间的一系列自定义日期日期。在日期范围日历上。 $dates = ['02/01/2017','03/01/2017'...直到] 这些日期。有可能吗?
      • 就像在 datepicker 中一样,我们可以为此使用 :beforeShowDay 函数,在 date-range-picker 中有什么:daterangepicker.com
      【解决方案7】:

      如果您想在 jquery datepicker 中禁用特定日期,那么这里有一个简单的演示。

      <script type="text/javascript">
          var arrDisabledDates = {};
          arrDisabledDates[new Date("08/28/2017")] = new Date("08/28/2017");
          arrDisabledDates[new Date("12/23/2017")] = new Date("12/23/2017");
          $(".datepicker").datepicker({
              dateFormat: "dd/mm/yy",
              beforeShowDay: function (date) {
                  var day = date.getDay(),
                          bDisable = arrDisabledDates[date];
                  if (bDisable)
                      return [false, "", ""]
              }
          });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        相关资源
        最近更新 更多