1

在 Tempus Dominus Bootstrap 4 datetimepicker 中选择日期后,如何以编程方式切换到时间视图?

这是代码:

$("#datetimepicker1").on("change.datetimepicker", function (e) {
  // After the date is selected, I would like to switch to time view automatically / programmatically
});

我在文档和网络中搜索,但找不到答案。谢谢!

4

1 回答 1

0

好,

尽管这个问题已经很老了,但我不会说这是不可能的,并且需要修改库。

这是工作代码:

<div class="container" style="padding: 80px;">
  <div class="row">
    <div class="col-sm-6">
      <div class="form-group">
        <div class="input-group date" id="datetimepicker1" data-target-input="nearest">
          <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker1" />
          <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker">
            <div class="input-group-text"><i class="fa fa-calendar"></i></div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
$(function() {
  $("#datetimepicker1").datetimepicker();
  var $picker = $("#datetimepicker1");
  $picker.on("change.datetimepicker", function (e) {
    // After the date is selected, I would like to switch to time view automatically / programmatically
    if (!e.oldDate && $picker.datetimepicker('useCurrent')) {
      // First time ever. If useCurrent option is set to true (default), do nothing
      // because the first date is selected automatically.
      return;
    }
    else if (
      e.oldDate &&
      e.date &&
      (e.oldDate.format('YYYY-MM-DD') === e.date.format('YYYY-MM-DD'))
    ) {
      // Date didn't change (time did).
      return;
    }

    setTimeout(function () {
      $('#datetimepicker1 [data-action="togglePicker"]').click();
    }, 300); // With a mini delay so that the animation doesn't fire right-away.
  });
});

这是工作的JSFiddle

我希望这可以帮助你。

于 2020-05-20T16:24:23.760 回答