我目前在我的代码中使用this datetimepicker(No Icon(仅限输入字段)),我需要在选择日期时间后触发一个函数。 我尝试使用几个jQuery函数,但我会得到不同的错误,这是我使用的:
$('#datetimepicker4').datetimepicker({
onClose: function (dateText, inst) {
alert("date chosen");
}
});
我收到此错误
TypeError:无法识别选项onClose!
和onSelect
类似的,
当我使用datepicker instead
$('#datetimepicker4').datepicker({
onClose: function (dateText, inst) {
alert("date chosen");
}
});
我收到此错误:
TypeError:$(...)。datepicker不是函数
ps:我正在使用jQuery 1.11.3并使用firefox运行代码。
编辑:问题已通过dp.change
答案 0 :(得分:5)
Eonasdan datetimepicker没有onClose
选项,您可以为dp.change
事件添加处理程序:
更改日期时触发。
参数:
e = { date, //date the picker changed to. Type: moment object (clone) oldDate //previous date. Type: moment object (clone) or false in the event of a null }
这是一个实时样本:
$('#datetimepicker4').datetimepicker()
.on('dp.change', function(e){
if(e.date){
console.log('Date chosen: ' + e.date.format() );
}
});

<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<link href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/css/bootstrap-datetimepicker.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/js/bootstrap-datetimepicker.min.js"></script>
<div class="container">
<div class="row">
<div class='col-sm-6'>
<input type='text' class="form-control" id='datetimepicker4' />
</div>
</div>
</div>
&#13;