Bootstrap Datepicker datesDisabled,动态生成的数组不起作用

时间:2017-09-22 10:08:22

标签: jquery bootstrap-datepicker

使用此Snippet,我将日期数组存储在一个变量中的两个日期之间"禁用"禁用引导日期选择器中的日期。

var startdate1 = $(".startdate1").text();
var enddate1 = $(".enddate1").text();

var daysBetweenDates = function(startdate1, enddate1) {
  var now = startdate1, dates = [];
  while (now.isBefore(enddate1) || now.isSame(enddate1)) {
    dates.push(now.format("\"D.M.YYYY\""));
    now.add('days', 1);
  }
  return dates;
};

var fromDate = moment(startdate1,"DD.MM.YYYY");
var toDate   = moment(enddate1,"DD.MM.YYYY");
var results  = daysBetweenDates(fromDate, toDate).join(', ');

$('#results').html(results);
var disabled = results;

前端的输出是: " 15.2.2018"," 16.2.2018"," 17.2.2018"," 18.2.2018"," 19.2。 2018年"," 20.2.2018"," 21.2.2018"," 22.2.2018"," 23.2.2018",& #34; 24.2.2018"," 25.2.2018"," 26.2.2018"

只有在我的datepicker实例中硬编码日期数组而不是其中的变量时,Bootstrap datepicker才会禁用日期:

$('.bdates').datepicker({
  format:                "dd.mm.yyyy",
  keyboardNavigation:    false,
  daysOfWeekDisabled:    "2,3,4,5,6",
  daysOfWeekHighlighted: "0,1",
  calendarWeeks:         true,
  autoclose:             true,
  todayHighlight:        true,
  updateViewDate:        false,
  datesDisabled:         disabled,
  startDate:             startdate,
  endDate:               enddate,
  language:              "de"
});

看看这个小提琴jsfiddle.net/8k1um9k9/

出了什么问题?请有人给我一个解决方案吗?! 谢谢!

1 个答案:

答案 0 :(得分:1)

您可以尝试使用下面的更新代码吗?所做的更改在代码中使用//Changed here进行了注释。基本上,您需要将日期数组传递给datesDisabled

var daysBetweenDates = function (startdate1, enddate1) {
    var now = startdate1, dates = [];
    while (now.isBefore(enddate1) || now.isSame(enddate1)) {
        dates.push(now.format("D.M.YYYY")); //Changed here
        now.add('days', 1);
    }
    return dates;
};

var fromDate = moment(startdate1, "DD.MM.YYYY");
var toDate = moment(enddate1, "DD.MM.YYYY");
var results = daysBetweenDates(fromDate, toDate); //Changed here
var disabled = results; //Changed here

$('#results').html(results.join(', ')); //Changed here
//var disabled = results;

//this datepicker is not working with disabled dates variable
$('.bdate').datepicker({
    format: "dd.mm.yyyy",
    keyboardNavigation: false,
    //      forceParse: false,
    daysOfWeekDisabled: "2,3,4,5,6",
    daysOfWeekHighlighted: "0,1",
    calendarWeeks: true,
    autoclose: true,
    todayHighlight: true,
    //updateViewDate: false,
    datesDisabled: disabled, //Changed here
    startDate: startdate,
    endDate: enddate,
    //language: "de"
});