[英]Trigger an event when selecting a date/time with datetime-picker
我目前在代碼中使用此 datetimepicker(無圖標(僅輸入字段)),並且一旦選擇了日期時間,就需要觸發一個函數。 我嘗試使用幾種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
解決
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.