使用 jQuery DatePicker 动态更改 minDate 和 maxDate [英] Changing minDate and maxDate on the fly using jQuery DatePicker
问题描述
我在使用 jQuery Datepicker 时遇到了一个特殊问题.我可以轻松添加日期范围,但我希望可选范围根据用户选择的事件进行更改.
I'm having a particular issue with the jQuery Datepicker. I can easily add a date range, but I want the selectable range to change depending on the event the user has picked.
因此,如果他们选择事件 #1,他们只能从事件 #1 的日期范围中选择日期.
So if they pick event #1, they can only pick dates from event #1's date range.
我编写了一个简单的小函数,每当用户选择一个新事件时都会调用该函数,但它只显示最初设置的 minDate/maxDate 值.
I've written a simple little function that gets called whenever the user selects a new event, but it only ever shows the initially set minDate/maxDate values.
function datepicker(startDate, endDate) {
$( "#date" ).datepicker({
inline: true,
minDate: new Date(startDate),
maxDate: new Date(endDate),
dateFormat: 'dd/mm/yy'
});
}
在再次调用上述函数之前,我已尝试调用 $('#date').datepicker('remove');
,以查看它是否创建了一个具有正确日期的新实例,但它似乎不起作用.
I've tried calling $('#date').datepicker('remove');
before calling the above function again, to see if it creates a new instance with the correct dates, but it doesn't seem to work.
我已经通过开发者工具检查了所有数据,一切都被正确调用和传递.有什么我可以做的事情来使这项工作如我所愿吗?
I've checked all the data through developer tools and everything is being called and passed correctly. Is there anything I can do to make this work how I want it to?
推荐答案
您有几个选择...
1) 你需要调用 destroy()
方法而不是 remove()
所以...
1) You need to call the destroy()
method not remove()
so...
$('#date').datepicker('destroy');
然后调用您的方法来重新创建 datepicker
对象.
Then call your method to recreate the datepicker
object.
2) 你可以通过
$('#date').datepicker('option', 'minDate', new Date(startDate));
$('#date').datepicker('option', 'maxDate', new Date(endDate));
或...
$('#date').datepicker('option', { minDate: new Date(startDate),
maxDate: new Date(endDate) });
这篇关于使用 jQuery DatePicker 动态更改 minDate 和 maxDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!