如何清除/重置 jQuery UI Datepicker 日历上的选定日期? [英] How do I clear/reset the selected dates on the jQuery UI Datepicker calendar?
问题描述
问题是当我清除日期(通过删除文本框值)时,仍然应用以前的日期限制.
The problem is that when I clear the dates (by deleting the textbox values), the previous date restrictions are still applied.
我一直在整理文档,但没有一个解决方案跳出来.我也无法在 SO/google 搜索中找到快速修复
I've been sorting through the documentation and nothing jumped out as a solution. I also wasn't able to find a quick fix on a SO/google search
http://jsfiddle.net/CoryDanielson/tF5MH/
解决方案:
// from & to input textboxes with datepicker enabled
var dates = $("input[id$='dpFrom'], input[id$='dpTo']");
// #clearDates is a button to clear the datepickers
$('#clearDates').on('click', function(){
dates.attr('value', '');
dates.each(function(){
$.datepicker._clearDate(this);
});
});
_.clearDate() 是 DatePicker 对象的私有方法.你不会在 jQuery UI 网站的公共 API 中找到它,但它的作用就像一个魅力.
_.clearDate() is a private method of the DatePicker object. You won't find it in the public API on jQuery UI's website, but it works like a charm.
推荐答案
我正试图完成这件事,即清空日期选择器,以便可以删除用户输入的过滤器.谷歌搜索了一下,我发现了这段甜蜜的代码:
I was trying to accomplish this very thing, that is, empty the datepicker so that filters entered by the user could be removed. Googling a bit I found this sweet piece of code:
您甚至可以在只读字段上添加清除功能.只需将以下代码添加到您的日期选择器:
You can add the clear feature even on read only fields. Just add the following code to your datepicker:
}).keyup(function(e) {
if(e.keyCode == 8 || e.keyCode == 46) {
$.datepicker._clearDate(this);
}
});
人们将能够突出显示(甚至只读字段),然后使用退格键或删除键使用 _clearDate
函数删除日期.
People will be able to highlight (even Read Only fields) and then use backspace or delete key to remove the date using _clearDate
function.
这篇关于如何清除/重置 jQuery UI Datepicker 日历上的选定日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!