Bootstrap日期选择器3始终打开

时间:2018-07-04 07:10:34

标签: datepicker datetimepicker bootstrap-datetimepicker eonasdan-datetimepicker

我希望引导日期选择器3始终保持打开状态。我已经尝试过所有以前的解决方案,但是我猜它们不适用于版本3。我可以在'dp.hide'上显示它,但是由于效率低下,它会挂起我的浏览器。

任何其他解决方案都是可行的。我不希望在浏览器上单击其他位置时关闭日历。

这是JavaScript代码

<script type="text/javascript">
    $(function () {
        $('#datetimepicker10').datetimepicker({
            viewMode: 'years',
            format: 'MM/YYYY',
            keepOpen: true
        }).on('dp.update', function () {
            $('#datetimepicker10').datetimepicker("show");
        });

        $('#datetimepicker10').data("DateTimePicker").show();

    });
</script>

这是html

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

2 个答案:

答案 0 :(得分:0)

代替使用input,而是在div中初始化日期,并将inline设置为true

$("#datetimepicker").datetimepicker({
    format: 'DD/MM/YYYY HH',
    inline: true,
    sideBySide: true
});
function showDate()
{
   console.log($('#datetimepicker').data("DateTimePicker").date());
}
.date
{
    max-width:200px
}
<link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/master/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<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.1/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
<script src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/master/src/js/bootstrap-datetimepicker.js"></script>

<div class="date" id="datetimepicker"></div>
<button onclick="showDate()">Get Date</button>

如果需要始终显示多个datetimepicker,请在初始化它们之前设置$.fn.datetimepicker.defaults.inline = true;。在这种情况下,无需在inline: true中传递options

注意:请谨慎设置格式。如果格式不包含时间,则不会显示时间成分。如果未设置,没问题。

答案 1 :(得分:0)

如果您不想使用inline mode,则可以始终使用debug选项。 有些让我讨厌的东西,但如果它对您有用...

$('#myDatepicker').datetimepicker({
  debug: true
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/25c11d79/build/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />


<br/>
<!--Space for the fiddle-->
<div class="container">
  <div class="row">
    <div class='col-sm-6'>
      <div class="form-group">
        <div class='input-group date' id='myDatepicker'>
          <input type='text' class="form-control" />
          <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
          </span>
        </div>
      </div>
    </div>
  </div>
</div>