jQuery UI 日期选择器重置日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16895056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:59:50  来源:igfitidea点击:

jQuery UI datepicker reset date

javascriptjqueryuser-interfacedatepicker

提问by J.Pip

I have a datepickerthat'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 datepickerstill 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?

datepicker在输入字段上有一个,我第一次打开它时的默认日期是今天的日期,这就是我想要的。
但是,当我选择一个日期并清除输入字段时,上面datepicker仍然有选定的日期,这是我不想要的

有没有人知道为什么会发生这种情况以及如何防止这种行为?

回答by silkfire

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

重置 Datepicker 小部件日期的正确方法是这样的:

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

Or like this:

或者像这样:

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

Whichever works best for you.

哪个最适合你。

回答by Alex Gill

回答by Shahbaz Ahmad

Clear button in calendar.

日历中的清除按钮。

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

回答by PIYUSH Itspk

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

即使字段处于只读模式,您也可以使用退格键清除字段。来源

回答by David Bradley

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

只是做最后一行结果在先前选择的年份中显示在日期选择器中