Javascript jQueryUI DatePicker YearRange 和 ChangeYear 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4510395/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:42:57  来源:igfitidea点击:

jQueryUI DatePicker YearRange and ChangeYear problems

javascriptjqueryjquery-uidatepickerjquery-ui-plugins

提问by CoreyT

I have a datepicker on an application form. We only allow applications where the date of birth is within a certain age range. I enabled ChangeYear and set the YearRange to "-65:-16". The problems I am having are as follows:

我在申请表上有一个日期选择器。我们只允许出生日期在特定年龄范围内的申请。我启用了 ChangeYear 并将 YearRange 设置为“-65:-16”。我遇到的问题如下:

1 - When I select a date without first selecting something in the year dropdown, I get the correct month and day, but I get 2016 as a year.

1 - 当我在没有首先在年份下拉列表中选择某些内容的情况下选择日期时,我得到了正确的月份和日期,但我得到了 2016 年。

2 - Trying to fix this I set the YearRange to "n-65:n-16". That causes the year dropdown to only display the current year (2010). Even weirder is that if you select a date, you still get the correct month and day, and the year 2016.

2 - 为了解决这个问题,我将 YearRange 设置为“n-65:n-16”。这会导致年份下拉列表仅显示当前年份(2010 年)。更奇怪的是,如果你选择一个日期,你仍然会得到正确的月份和日期,以及 2016 年。

Here is the code I use to setup the datepicker:

这是我用来设置日期选择器的代码:

    <script type="text/javascript">
    $(document).ready(function (e) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

        function EndRequestHandler(sender, args) {
            $(function () {
                $("#DateOfBirth").datepicker({ yearRange: '-65:-13', changeMonth: true, changeYear: true, defaultDate: '1-1-1994', dateFormat: 'mm-dd-yy' })
            });
        }

        $(function () {
            $("#DateOfBirth").datepicker({ yearRange: '-65:-13', changeMonth: true, changeYear: true, defaultDate: '1-1-1994', dateFormat: 'mm-dd-yy' })
        });
    });
</script>

I am hoping this is something I have done wrong, and someone can tell me what that is. Thanks for any help.

我希望这是我做错的事情,有人可以告诉我那是什么。谢谢你的帮助。

回答by Karsten

Try using 'c' for the current year like:

尝试在当前年份使用“c”,例如:

$('.datepicker').datepicker({changeYear: true, yearRange : 'c-65:c+10'});

for more information view http://api.jqueryui.com/datepicker/#option-yearRange

有关更多信息,请查看http://api.jqueryui.com/datepicker/#option-yearRange

回答by Ben Koehler

Check your defaultDate setting. I can only see what you're describing when I change the default date to something outside of the specified year range (i.e. '1-1-2016'.) Everything else in the code you posted worked for me.

检查您的 defaultDate 设置。当我将默认日期更改为指定年份范围之外的日期(即“1-1-2016”)时,我只能看到您所描述的内容。您发布的代码中的所有其他内容都对我有用。

回答by Mark van der Gugten

Stumbled upon this as well. I did not research it further, but the call order seems to make a difference.

也偶然发现了这一点。我没有进一步研究,但调用顺序似乎有所不同。

this works:

这有效:

$('DIV#startdate').datepicker( 'option', { dateFormat: 'yy-mm-dd' } );
$('DIV#startdate').datepicker( 'setDate', '10-09-11' );
$('DIV#startdate').datepicker( 'option', { changeYear: true } );

this shows the year listbox with only 2010:

这显示了只有 2010 年的年份列表框:

$('DIV#startdate').datepicker( 'option', { dateFormat: 'yy-mm-dd' } );
$('DIV#startdate').datepicker( 'option', { changeYear: true } );
$('DIV#startdate').datepicker( 'setDate', '10-09-11' );

回答by Sasmit

Try giving the range with actual year values. e.g.

尝试给出具有实际年份值的范围。例如

$('.datepicker').datepicker({changeYear: true, yearRange : '1990:2010'})

They should also work with any dateformat.

他们还应该使用任何日期格式。

回答by Vijay Porwal

Try using the maxDate and minDate option like:

尝试使用 maxDate 和 minDate 选项,例如:

$(document).ready(function () {
    $("#datepicker").datepicker({
        yearRange: '-65:-13',
        changeMonth: true,
        changeYear: true,
        defultDate: '1-1-1994',
        dateFormat: 'mm-dd-yy',
        minDate:"-65Y",
        maxDate:"-13Y" 
    });
});