gpt4 book ai didi

javascript - daterangepicker 未设置日期

转载 作者:行者123 更新时间:2023-12-02 23:16:27 24 4
gpt4 key购买 nike

我的网站上有一个日期范围选择器 ( http://www.daterangepicker.com/ ) 设置:

enter image description here

使用此代码:

$(function() {
$('input[id="searchDateRange"]').daterangepicker({
timePickerSeconds: true,
autoApply: true,
startDate: "${defaultStart}",
endDate: "${defaultEnd}",
timePicker: true,
locale: {
format: 'MM/DD/YYYY HH:mm:ss:SSS'
},
ranges: {
'Past 24 Hours': [moment().subtract(1, 'days'), moment()],
'Today': [moment().startOf('day'), moment().endOf('day')],
'Yesterday': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
'None': [null, null]
}
});


var history = document.querySelector('.history');
if (history) {
new Tablesort(history);
}
});

当我选择“无”时,我希望它将字段设置为空白,但它会将值设置为“无效日期 - 无效日期”:

enter image description here

有没有办法根据选择清空该字段?

最佳答案

使用apply.daterangepicker事件和时刻isValid()

$('input[id="searchDateRange"]').daterangepicker({
timePickerSeconds: true,
autoApply: true,
//startDate: "${defaultStart}",
//endDate: "${defaultEnd}",
timePicker: true,
locale: {
format: 'MM/DD/YYYY HH:mm:ss:SSS'
},
ranges: {
'Past 24 Hours': [moment().subtract(1, 'days'), moment()],
'Today': [moment().startOf('day'), moment().endOf('day')],
'Yesterday': [moment().subtract(1, 'days').startOf('day'), moment().subtract(1, 'days').endOf('day')],
'Last 7 Days': [moment().subtract(6, 'days'), moment()],
'Last 30 Days': [moment().subtract(29, 'days'), moment()],
'This Month': [moment().startOf('month'), moment().endOf('month')],
'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
'None': [null, null]
}
});

$('input[id="searchDateRange"]').on('apply.daterangepicker', function(ev, picker) {
if (!picker.startDate.isValid() || !picker.endDate.isValid()) {
$(this).val('')
}
});
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<input type="text" id="searchDateRange" />

关于javascript - daterangepicker 未设置日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57146425/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com