简体   繁体   中英

jQuery UI datepicker reset date

I have a datepicker that's on an input field, default date when I first open it is today's date, which is what I want.
However, when I select a date and I clear the input field, the datepicker still has the selected date on it, which I don't want

Does anyone have an idea why this happens and how I can prevent this behavior?

The proper way to reset the date of a Datepicker widget is like this:

$.datepicker._clearDate('#input_field_goes_here');

Or like this:

$('#input_field_goes_here').datepicker('setDate', null);

Whichever works best for you.

http://codepen.io/alexgill/pen/yOQrwV

$(SELECTOR).datepicker('setDate', null);

Clear button in calendar.

 $("#txtCalendar").datepicker({
        showButtonPanel: true,
        closeText: 'Clear',
        onClose: function (dateText, obj) {
            if ($(window.event.srcElement).hasClass('ui-datepicker-close'))
                $("#txtCalendar").val('');
        }
    });

Just add this code

}).keyup(function(e) {
    if(e.keyCode == 8 || e.keyCode == 46) {
        $.datepicker._clearDate(this);
    }
});

You can use backspace to clear field even if it has in read only mode. Source

To really reset everything, including the selected year in the date selector popup, I had to do the following:

$(SELECTOR).datepicker('setDate', new Date());
$(SELECTOR).datepicker('setDate', null);

Just doing the last line results in the previously selected year to show in the date selector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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