【问题标题】:Disable specific dates and enable specific days of the week in jQuery datepicker在 jQuery datepicker 中禁用特定日期并启用一周中的特定日期
【发布时间】:2018-05-29 20:32:31
【问题描述】:

我正在尝试在 jQuery UI 日期选择器上禁用特定日期并仅启用一周中的特定日期。

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

My objective is to enable only chosen days of the week to be available, but when a date is chosen and bought, I need to disable it too.我成功地禁用了我需要禁用的星期几,但我什至无法制作关于应该禁用的购买日期的硬编码原型。

代码(带有一些硬编码示例):

$(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(),
            });
        }
    }
});

【问题讨论】:

  • 所以问题是如何在页面某处被选中后禁用额外的天数?或者如果不是,bought day 来自哪里?
  • 它将来自数据库。我希望至少可以使用硬代码进行测试。
  • 控制台中是否显示任何错误?

标签: javascript jquery jquery-ui datepicker


【解决方案1】:

请查看: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" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2019-04-25
    • 2011-02-27
    • 2012-04-02
    • 2010-10-15
    • 1970-01-01
    • 2016-02-13
    相关资源
    最近更新 更多