简体   繁体   English

如果输入值仅包含时间,则在初始化 bootstrap datetimepicker 时出现 JS 错误

[英]JS error when initializing bootstrap datetimepicker if input value contains only time

I'm using DateTime Picker and I want to load a only Time picker.我正在使用DateTime Picker并且我想加载一个唯一的时间选择器。

I initialize my input field like this and works good.我像这样初始化我的输入字段并且效果很好。 This let me pick only times jsfiddle :这让我只选择jsfiddle次:

<div class="input-group date" id="datetimepicker_hour1">
  <input type="text" id="hour_from" name="hour_from" class="form-control" />
</div>
$('#datetimepicker_hour1').datetimepicker({
  language: 'es',
  format: 'hh:ii',
  minuteStep: 60,
  autoclose: true,
  minView: 1,
  maxView: 1,
  startView: 1
});

If I want initialize this input with a time preselected, JS error occurs:如果我想用预选的时间初始化这个输入,就会发生 JS 错误:

"Uncaught TypeError: Cannot read property 'getTime' of undefined" “未捕获的类型错误:无法读取未定义的属性‘getTime’”

But if I put the input value like this 2017-12-24 13:00 , the input time picker have that full value.但是,如果我输入这样的输入值2017-12-24 13:00 ,则输入时间选择器具有完整的值。 That works good, but I want that the first value could be only the time, not full datetime.这很好用,但我希望第一个值只能是时间,而不是完整的日期时间。

Here is jsfiddle with a value preloaded.这是带有预加载值的jsfiddle I want that this value could be 13:00 :我希望这个值可以是13:00

<div class="input-group date" id="datetimepicker_hour1">
  <input type="text" id="hour_from" name="hour_from" class="form-control" value="2017-12-24 13:00" />
</div>
$('#datetimepicker_hour1').datetimepicker({
  language: 'es',
  format: 'hh:ii',
  minuteStep: 60,
  autoclose: true,
  minView: 1,
  maxView: 1,
  startView: 1
});

Let's do a little trick.让我们做一个小技巧。 First we'll hide the input field.首先,我们将隐藏输入字段。 Then we'll devide the datetime value by space character to get only the time and set this as the input value.然后我们将按空格字符划分日期时间值以仅获取时间并将其设置为输入值。 After that we will show the input field.之后,我们将显示输入字段。

 var $hour_from = $("#hour_from"); $hour_from.datetimepicker({ language: 'es', format: 'hh:ii', minuteStep: 60, autoclose: true, minView: 1, maxView: 1, startView: 1, timeFormat: 'hh:mm', }); $hour_from.val($hour_from.val().split(' ')[1]) $hour_from.show();
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://rawgit.com/AuspeXeu/bootstrap-datetimepicker/master/js/bootstrap-datetimepicker.js"></script> <link href="https://rawgit.com/AuspeXeu/bootstrap-datetimepicker/master/css/bootstrap-datetimepicker.css" rel="stylesheet"/> <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-theme.min.css" rel="stylesheet"/> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="input-group date" id="datetimepicker_hour1"> <input type="text" id="hour_from" name="hour_from" class="form-control" value="2017-12-24 13:00" data-date-format="hh:ii" style="display:none;" /> </div>

Set the value in the inputfield after datetimepicker has been initialized, then everything works.datetimepicker初始化后在输入字段中设置值,然后一切正常。

var hour_from = "13:00";

<div class="input-group date" id="datetimepicker_hour1">
  <input type="text" id="hour_from" name="hour_from" class="form-control" value="" />
</div>

$('#datetimepicker_hour1').datetimepicker({
  language: 'es',
  format: 'hh:ii',
  minuteStep: 60,
  autoclose: true,
  minView: 1,
  maxView: 1,
  startView: 1
});

$('#hour_from').val(hour_from);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM