0

flatpickr在我的应用程序上用作日期时间选择器。此日期选择器允许选择多个日期。我得到了我最后选择的日期(选择的日期看起来像标记的)。但问题是当我从多个日期中随机选择一个日期时,我无法获取该日期。

这是我的 jQuery 代码

$("#available-calendar").flatpickr({
    disableTime: true,
    dateFormat: "Y-m-d",
    inline: true,
    mode: "multiple",
    minDate: "today",

    defaultDate: newDates,
    onChange: function(selectedDates, dateStr, instance) {
        var selectedDates = dateStr.split(",");
        var latestDate = selectedDates[selectedDates.length-1];
        console.log(latestDate);
        alert(latestDate);
    },
});

newDates是从数据库中获取的日期数组。

4

1 回答 1

0

恐怕从flatpickr现在开始就没有支持方法了。我会给你一个技巧,通过将日期存储在global variable. 它可能适用于您的情况

$(() => {
  let defaultDate = ["2018-06-20", "2018-06-22"]
  let selected = _.cloneDeep(defaultDate)
  $("#test").flatpickr({
    disableTime: true,
    dateFormat: "Y-m-d",
    inline: true,
    mode: "multiple",
    minDate: "today",
    defaultDate: defaultDate,
    onChange: function(selectedDates, dateStr, instance) {

      let newDate = _.chain(dateStr)
        .split(',')
        .map(_.trim)
        .value();
      if (newDate.length < selected.length ) {
        let deselected = _.difference(selected, newDate);
        console.log(deselected)
      } 
      selected = _.cloneDeep(newDate)
    }
  });
});

演示 => https://codepen.io/anon/pen/zaEGOK

于 2018-06-15T07:50:03.580 回答