javascript jquery datepicker 年份范围默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12407786/
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
jquery datepicker year range default
提问by CMR
Having a bit of an issue with the JQuery datePicker
, I suspect it's just the matter of a setting I've overlooked or got wrong.
JQuery 有点问题datePicker
,我怀疑这只是我忽略或出错的设置问题。
If you look at this simple fiddle: JS Fiddle
如果你看看这个简单的小提琴:JS Fiddle
You'll see I've set the year range, so that the by default when you click on the input it will open it up on 1994, but if you then click on any of those dates, e.g. 3rd Sept, it'll actually put it in the input as 2012 still, rather than the year that is selected in the drop down menu.
你会看到我已经设置了年份范围,所以默认情况下,当你点击输入时,它会在 1994 年打开它,但是如果你点击这些日期中的任何一个,例如 9 月 3 日,它实际上会将其作为 2012 仍然放在输入中,而不是在下拉菜单中选择的年份。
How can I make it so that it uses the correct year without having to change the drop down and then change it back again?
我怎样才能使它使用正确的年份而不必更改下拉菜单然后再次更改?
Cheers.
干杯。
采纳答案by vonflow
回答by JasCav
As others have suggested, you have to set the defaultDate. However, unlike the other solutions, you don't want to hardcode the date since your dropdown list will be updating as the years pass since you're doing a relative list.
正如其他人所建议的那样,您必须设置 defaultDate。但是,与其他解决方案不同,您不希望对日期进行硬编码,因为您的下拉列表会随着时间的推移而更新,因为您正在执行相关列表。
Try this instead...
试试这个...
function loadDatePicker() {
$('.datePicker').datepicker({
dateFormat: 'yy-mm-dd',
changeYear: true,
changeMonth: true,
yearRange: "-18:-12",
defaultDate:"-18y-m-d" // Relative year/month/day
});
}
回答by Jageen
I think I got the reason.. It is because yearRange: just restrict the range in dropdown menu only. It just change the list nothing else.
我想我明白了。这是因为 yearRange: 只限制下拉菜单中的范围。它只是更改列表而已。
They mansion this over here..
他们把这个豪宅在这里..
http://jqueryui.com/demos/datepicker/#option-yearRange[Note that this option only affects what appears in the drop-down, to restrict which dates may be selected use the minDate and/or maxDate options.]
http://jqueryui.com/demos/datepicker/#option-yearRange[请注意,此选项仅影响下拉列表中显示的内容,使用 minDate 和/或 maxDate 选项限制可以选择的日期。]
To Prove this .. set date picker as below and you will find all date disable. because it sawing us the calander of 2012 not 1994
为了证明这一点......设置日期选择器如下,你会发现所有日期都被禁用。因为它给我们锯的是 2012 年的日历,而不是 1994 年
$('.datePicker').datepicker({
dateFormat: 'yy-mm-dd',
changeYear: true,
changeMonth: true,
yearRange: "-18:-12",
maxDate: new Date(2012,1-1,1)
});
To fix it you have to use defaultDate as well as minDate and maxDate
要修复它,您必须使用 defaultDate 以及 minDate 和 maxDate
i hope i am clear
我希望我很清楚