如何清除/重置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/
解决方案:
Solution:
// 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.
推荐答案
我正在尝试完成此操作,即清空datepicker,以便删除用户输入的过滤器.谷歌搜索了一下,我发现了这段甜蜜的代码:
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
函数使用退格键或Delete键删除日期.
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屋!