在 JQuery UI DatePicker 中动态更改选项失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4373072/
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
Change option dynamically in JQuery UI DatePicker fails
提问by v14nt0
I want to make date range selection using jquery-ui datepicker. First change at #dteStart succeed to set minDate at #dteEnd.
But #dteEnd failed to refresh its options on next change, if i alert
DateOptions.minDate
its value changed according to dateMin.
我想使用 jquery-ui datepicker 进行日期范围选择。#dteStart 处的第一次更改成功设置了 #dteEnd 处的 minDate。但是#dteEnd 未能在下一次更改时刷新其选项,如果我DateOptions.minDate
根据 dateMin提醒
其值已更改。
Maybe i miss something here...
也许我在这里想念一些东西......
$(document).ready(function ()
{
$("#dteStart").datepicker()
.change(function ()
{
dateStart = $(this).datepicker('getDate');
dateMin = new Date(dateStart.getTime());
dateMin.setDate(dateMin.getDate() + 1);
var DateOptions = {
dateformat: "mm/dd/yyyy",
minDate: dateMin
}
$("#dteEnd").datepicker(DateOptions);
});
});
TIA,
TIA,
REV
转速
回答by TheVillageIdiot
put $("#dteEnd").datepicker("destroy");
before $("#dteEnd").datepicker(DateOptions);
and it will work fine.
放在$("#dteEnd").datepicker("destroy");
之前$("#dteEnd").datepicker(DateOptions);
,它会正常工作。
回答by Torge
If you just want to change the already configured options, you can also do:
如果只想更改已配置的选项,还可以执行以下操作:
$("#dteEnd").datepicker("option", DateOptions);
or
或者
$("#dteEnd").datepicker("option", { dateFormat: "mm/dd/yyyy" });
回答by bencergazda
The following jQuery helper function may be useful in such cases to preserve the original options:
在这种情况下,以下 jQuery 辅助函数可能有助于保留原始选项:
$.fn.customizeDatepicker = function(newOptions) {
var prevOptions = $(this).datepicker('option', 'all');
$(this).datepicker('destroy').datepicker($.extend(prevOptions, newOptions));
return this;
};
It saves the previous options and extends them with the new options.
它保存以前的选项并使用新选项扩展它们。