简体   繁体   English

Daterangepicker 清除 UI datepicker 选择的日期

[英]Daterangepicker Clear UI datepicker selected date

The below example Daterangepikcer is working fine.下面的示例 Daterangepikcer 工作正常。 Problem with when clearing calendar only.仅清除日历时出现问题。 When I click clear button it's only clearing the input field is empty.当我单击clear按钮时,它只会清除输入字段为空。 but I want to clear the daterangepicker calendar as we see when open at the first time.我想清除第一次打开时看到的 daterangepicker 日历

I tried this but it's not clearing the selected date.我试过了,但它没有清除选定的日期。 Thanks in Advance.提前致谢。

$(this).val('');

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script> <script src="http://www.daterangepicker.com/daterangepicker.js"></script> <link href="http://www.daterangepicker.com/daterangepicker.css" rel="stylesheet"/> <input type="text" name="datefilter" value="" /> <script type="text/javascript"> $(function() { $('input[name="datefilter"]').daterangepicker({ autoUpdateInput: false, locale: { cancelLabel: 'Clear' } }); $('input[name="datefilter"]').on('apply.daterangepicker', function(ev, picker) { $(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY')); }); $('input[name="datefilter"]').on('cancel.daterangepicker', function(ev, picker) { $(this).val(''); }); }); </script>

Initially when you have the input empty and you are going to select a range, the current day appears marked in blue.最初,当您将输入为空并且您将转到 select 一个范围时,当前日期显示为蓝色。 You can replicate this behavior with this code:您可以使用以下代码复制此行为:

$('input[name="datefilter"]').on('cancel.daterangepicker', function(ev, picker) {
            $(this).val('');
            $('input[name="datefilter"]').data('daterangepicker').setStartDate(moment());
            $('input[name="datefilter"]').data('daterangepicker').setEndDate(moment());
        });

When the Clear button is pressed, the input will be empty.当按下清除按钮时,输入将为空。

If you click on the input to select a range, the current day will be shown again in blue (the previous range will not be shown).如果您点击输入到 select 一个范围,当天将再次以蓝色显示(之前的范围将不会显示)。

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

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