[英]How to stop datetimepicker setting todays date in textbox by default onclick
當我們選擇日歷圖標時,它會自動將今天的日期設置為文本框。 datetimepicker()函數中是否有任何參數/選項可以設置為false或null,以防止datetimepicker()默認設置今天日期到文本框。 如果有人沒有從日歷中選擇任何日期,則文本框應為空,而不是由datetimepicker()設置今天的日期。
值得一讀的文檔。 在初始化datetimepicker時更改useCurrent
屬性。
https://eonasdan.github.io/bootstrap-datetimepicker/Options/#usecurrent
如果你使用.datetimepicker()用於Bootstrap 4 + jQuery
當您單擊該元素時,可能會發生這種不期望的日期更新效果。
這是一個黑客,以避免使用當前日期點擊覆蓋當前值,並允許正常選擇選擇器。
對於Tempus Dominus版本: https : //tempusdominus.github.io/bootstrap-4/
JS
var dateNow = new Date();
$('.datetimepicker').each(function() {
var _el = this;
$(this).datetimepicker({
autoclose: true,
allowInputToogle: true,
format: 'YYYY-MM-DD HH:mm',
defaultDate: dateNow,
});
// 'this.parent()' is the container and data-target | '_el' is the input
$(this).parent().on('change.datetimepicker', function(e) {
// HACK
if (!e.oldDate) return;
$(_el).val(e.date.format('YYYY-MM-DD HH:mm'));
});
});
HTML
<div class="form-group">
<label class="control-label"><i class="icon-right-open-outline"></i> Date</label>
<div class="input-group" id="form_datetimepicker_1">
<input name="date_selected"
id="date_selected"
class="form-control datetimepicker"
type="text"
data-toggle="datetimepicker"
value="2019-11-22 22:55:00"
data-target="#form_datetimepicker_1">
<div class="input-group-append" data-target="#form_datetimepicker_1" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.