获取日期时间输入字段的值

时间:2019-07-15 09:34:59

标签: javascript jquery

我想提醒输入的日期时间的值,但出现错误“ datetimepicker不是函数”,这是我使用的jQuery链接

var time_start = $('#datetimepicker1').val();
$('#datetimepicker1').datetimepicker();

$(document).on('change', '#datetimepicker1', function() {
  time_start = $(this).val();
})

$(document).on('click', '#btn_ok', function() {
  alert(time_start);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

<div class="form-inline">
  <label style="padding-right: 10px">Từ</label>
  <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>
  <br>
  <label style="padding-right: 10px; padding-left: 10px">đến</label>
  <div class="input-group date" id="datetimepicker2" data-target-input="nearest">
    <input type="text" class="form-control datetimepicker-input" data-target="#datetimepicker2" />
    <div class="input-group-append" data-target="#datetimepicker2" data-toggle="datetimepicker">
      <div class="input-group-text"><i class="fa fa-calendar"></i></div>
    </div>
  </div>
</div>

我该如何解决?

1 个答案:

答案 0 :(得分:1)

jquery ui datepicker的功能是datepicker,而不是datetimepicker。另外,您还将日期时间选择器功能附加到div而不是输入字段。我添加了一个onSelect事件,每次在日历中选择日期时都会调用该事件。

$('#datetimepicker1').datepicker({
  onSelect: function(dateText) {
    alert(dateText);
  }
});

$(document).on('click', '#btn_ok', function () {
   alert($('#datetimepicker1').val());
})
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

<div class="form-inline">
    <label style="padding-right: 10px">Từ</label>
    <div class="input-group date" data-target-input="nearest">
         <input type="text" id="datetimepicker1" class="form-control datetimepicker-input">
    </div>
    <button id="btn_ok">Submit</button>
</div>