在jQuery datepicker

时间:2018-05-29 20:32:31

标签: javascript jquery jquery-ui datepicker

我正在尝试禁用特定日期,并且只在jQuery UI datepicker上启用一周中的特定日期。

这是一个Wordpress / Woocommerce主题,我正在尝试解决一些错误,但我在这里几个小时试图找到解决方案,甚至尝试我在StackOverflow和其他网站上找到的每个解决方案,但似乎没有工作。

我的目标是只启用一周中选定的几天,但是当选择并购买日期时,我也需要禁用它。我成功地禁用了我需要禁用的一周中的日子,但是我甚至无法做一个关于应该禁用的已购买日期的硬编码原型。

代码(带有一些硬编码样本):

$(document).ready(function(){
    var available_days = ["3"]; //it comes from the database
    var today = new Date();
    var tour_start_date = new Date( 1525132800000 ); //it comes from the database
    var tour_end_date = new Date( 1546214400000 ); //it comes from the database
    var available_first_date = tour_end_date;
    var lang = 'en_UK';

    lang = lang.replace( '_', '-' );

    today.setHours(0, 0, 0, 0);
    tour_start_date.setHours(0, 0, 0, 0);
    tour_end_date.setHours(0, 0, 0, 0);

    if ( today > tour_start_date ) {
        tour_start_date = today;
    }

    function DisableDays(date) {
        var day = date.getDay();

        if ( available_days.length == 0 ) {
            if ( available_first_date >= date && date >= tour_start_date) {
                available_first_date = date;
            }
            return true;
        }

        if ( $.inArray( day.toString(), available_days ) >= 0 ) {
            if ( available_first_date >= date && date >= tour_start_date) {
                available_first_date = date;
            }
            return true;
        } else {
            return false;
        }
    }
    if ( $('input.date-pick').length ) {
        if ( lang.substring( 0, 2 ) != 'fa' ) {
            $('input.date-pick').datepicker({
                startDate: tour_start_date,
                endDate: tour_end_date,
                beforeShowDay: DisableDays,
                language: lang
            });
            $('input[name="date"]').datepicker( 'setDate', available_first_date );
        } else {
            var date_format = $('input.date-pick').data('date-format');
            $('input.date-pick').persianDatepicker({
                observer: true,
                format: date_format.toUpperCase(),
            });
        }
    }
});

1 个答案:

答案 0 :(得分:0)

请查看:http://api.jqueryui.com/datepicker/#option-beforeShowDay

  

一个以日期作为参数的函数,必须返回一个数组:

     

[0]:true / false表示此日期是否可选

     

[1]:要添加到日期单元格的CSS类名称或默认演示文稿的""

     

[2]:此日期的可选弹出工具提示

代码中的问题是,当您需要数组时,您只返回truefalse。例如:

$(function() {
  var available_days = ["3", "5"]; //it comes from the database
  var today = new Date();
  var tour_start_date = new Date(1525132800000); //it comes from the database
  var tour_end_date = new Date(1546214400000); //it comes from the database
  var available_first_date = tour_end_date;
  var lang = 'en_UK';

  lang = lang.replace('_', '-');

  today.setHours(0, 0, 0, 0);
  tour_start_date.setHours(0, 0, 0, 0);
  tour_end_date.setHours(0, 0, 0, 0);

  if (today > tour_start_date) {
    tour_start_date = today;
  }

  function DisableDays(date) {
    var day = date.getDay();
    var found = false;

    if (available_days.length == 0) {
      if (available_first_date >= date && date >= tour_start_date) {
        available_first_date = date;
      }
      found = true;
    }
    if ($.inArray(day.toString(), available_days) >= 0) {
    console.log(day + " in Array");
      if (available_first_date >= date && date >= tour_start_date) {
        available_first_date = date;
      }
      found = true;
    }
    console.log(day, found);
    return [found, "tour-date", "Tour Date"];
  }
  if ($('input.date-pick').length) {
    if (lang.substring(0, 2) != 'fa') {
      $('input.date-pick').datepicker({
        startDate: tour_start_date,
        endDate: tour_end_date,
        beforeShowDay: DisableDays,
        language: lang
      });
      $('input[name="date"]').datepicker('setDate', available_first_date);
    } else {
      var date_format = $('input.date-pick').data('date-format');
      $('input.date-pick').persianDatepicker({
        observer: true,
        format: date_format.toUpperCase(),
      });
    }
  }
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

Date: <input type="text" class="date-pick" name="date" />