3

我有一些使用 flatpickr 日历实例的 HTML。我想做的是在单击随附的跨度时仅打开特定的日历实例。

<div class="formRow">
    <div class="datetimepicker input-group date">
        <input id="initial-notification-date-time" name="initial-notification-date-time" type="text" class="form-control" />
        <span class="input-group-addon"></span>
    </div>
</div>

随附的 Javascript 是:

$('.datetimepicker input').flatpickr({
  dateFormat: 'm/d/Y',
  enableTime: true,
  defaultDate: new Date(),
  onReady: function() {
      var flatPickrInstance = this;
      console.log(flatPickrInstance);
      console.log($(".datetimepicker input").siblings(".datetimepicker span")); 
      $(".datetimepicker input").siblings(".datetimepicker span").click(function () {
          $(".datetimepicker input").flatpickr();
      });
  }
});

当前所做的是导致“.datetimepicker input”的所有实例重新初始化为新实例。我实际上如何导致我想要打开的实例?

4

1 回答 1

0

您每次都在重新初始化 flatpickr。你需要做这样的事情:

$('.datetimepicker input').flatpickr({
  dateFormat: 'm/d/Y',
  enableTime: true,
  defaultDate: new Date(),
  onReady: function() {
      var flatPickrInstance = this;
      var $flatPickrInput = $(flatPickrInstance.element);
      $flatPickrInput.siblings("span").click(function () {
          flatPickrInstance.toggle();
      });
  }
});
于 2016-08-25T03:16:12.680 回答