Bootstrap datetimepicker:当用户点击输入时显示日历弹出窗口

Bootstrap datetimepicker: show calendar pop-up when a user clicks on input

有没有办法在用户点击 input 时也显示日历?现在我必须单击日历图标,然后它会显示日历。

HTML:

<div class='input-group date' id='datetimepicker1'>
  <input type='text' class="form-control" />
  <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>

JavaScript:

$(function() {
  $('#datetimepicker1').datetimepicker();
});


示例:https://jsfiddle.net/8jpmkcr5/81/

这有点棘手,但您需要将日期时间选择器移至输入并手动处理对输入组插件的点击。例如:

$(function() {
  $('.datetimepicker').datetimepicker();
  
  $('.datetimepicker-addon').on('click', function() {
   $(this).prev('input.datetimepicker').data('DateTimePicker').toggle();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>


<div class='input-group date'>
  <input type='text' class="form-control datetimepicker" />
  <span class="input-group-addon datetimepicker-addon">
    <span class="glyphicon glyphicon-calendar"></span>
  </span>
</div>

您可以简单地使用 allowInputToggle 选项将其设置为 true(默认值:false)。正如文档所说:

If true, the picker will show on textbox focus and icon click when used in a button group

这是一个有效的现场样本:

$('#datetimepicker1').datetimepicker({
  allowInputToggle: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet"/>

<div class='input-group date' id="datetimepicker1">
    <input type='text' class="form-control" />
    <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
    </span>
</div>