问题描述
我在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!