在创建时自动显示bootstrap-datetimepicker

时间:2016-09-01 20:41:49

标签: jquery twitter-bootstrap bootstrap-modal eonasdan-datetimepicker

我有一个datetimepicker,我想在创建后立即自动打开。我已经尝试在创建后调用show方法,但是之后它不会被关闭!

例如,我直观地期望当我在外面点击或者失去焦点时,小部件会关闭。



// Set up form widgets
$("#input_completed_at").datetimepicker({
  sideBySide: true
});

$("#input_completed_at").data("DateTimePicker").show();

.modal-open .modal.modal-allow-overflow {
  overflow: visible;
}

<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.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.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.7/css/bootstrap.min.css" rel="stylesheet" />

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<div id="myModal" class='modal fade modal-allow-overflow' data-width="760">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
        <h4 class="modal-title">Pick a date!</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label for="input_completed_at">Task Completion Date</label>
          <div class="input-group date" id="input_completed_at">
            <input type="text" name="completed_at" class="form-control">
            <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

有没有更好的方法让它自动显示,但在失去焦点/点击外面时自动关闭?

更新:当我的datepicker在Bootstrap模式弹出窗口内时似乎只是一个问题。请参阅更新的示例。

2 个答案:

答案 0 :(得分:1)

您可以使用bootstrap modal events之类shown.bs.modal来显示模式显示时的日期时间选择器。

这是一个实例:

// Set up form widgets
$("#input_completed_at").datetimepicker({
  sideBySide: true
});

$('#myModal').on('shown.bs.modal', function (e) {
  $("#input_completed_at").data("DateTimePicker").show();
});
.modal-open .modal.modal-allow-overflow {
  overflow: visible;
}
<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.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.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.7/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" />

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Launch demo modal
</button>

<div id="myModal" class='modal fade modal-allow-overflow' data-width="760">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
        <h4 class="modal-title">Pick a date!</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label for="input_completed_at">Task Completion Date</label>
          <div class="input-group date" id="input_completed_at">
            <input type="text" name="completed_at" class="form-control">
            <span class="input-group-addon">
              <span class="glyphicon glyphicon-calendar"></span>
            </span>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

答案 1 :(得分:0)

它有点hacky但似乎在为我工作

<强> HTML

<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.7/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.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.7/css/bootstrap.min.css" rel="stylesheet" />

<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">   Launch demo modal </button>

<div id="myModal" class='modal fade modal-allow-overflow' data-width="760">   <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
        <h4 class="modal-title">Pick a date!</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label for="input_completed_at">Task Completion Date</label>
          <div class="input-group date" id="input_completed_at">
            <input type="text" name="completed_at" class="form-control">
            <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
            </span>
          </div>
        </div>
      </div>
    </div>   </div> </div>

<强> JS

// Set up form widgets
$("#input_completed_at").datetimepicker({
  sideBySide: true
});

$("#input_completed_at").data("DateTimePicker").show();
$( ".modal-header" ).click(function() {
  $("#input_completed_at").data("DateTimePicker").hide();
});

$( "#input_completed_at" ).click(function() {
  $("#input_completed_at").data("DateTimePicker").toggle();
});

修改 如果你想在modal init上打开它,你可以将一个单击绑定到按钮

$( ".btn" ).click(function() {
  $("#input_completed_at").data("DateTimePicker").show();
});

Codepen