2010-12-22 147 views
9

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

1 - 当我在未选择年份下拉列表中选择某个日期的情况下选择某个日期时,我得到正确的月份和日期,但我将2016年定为一年。

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

下面是我用它来设置的日期选择器代码:

<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> 

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

+0

什么版本的jQuery和jQuery UI您使用的是? – 2010-12-23 20:44:33

+0

我正在使用1.4.4和1.8.7,我也尝试过早期版本,结果相同。 – CoreyT 2011-01-07 18:45:03

回答

18

尝试使用 'C' 当年像:

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

更多资料查看http://api.jqueryui.com/datepicker/#option-yearRange

+0

我已经尝试了上面的代码,但在我的情况下它不起作用。对于已经添加的日期,日期字段预先填充为m/d/y ex:01/01/xxxx。 – sangam 2016-01-11 14:22:56

1

检查您的defaultDate设置。我只能看到您将默认日期更改为指定年份范围之外的内容(即'1-1-2016')。您发布的代码中的其他内容都适用于我。

1

也碰到过这个问题。我没有进一步研究,但通话顺序似乎有所作为。

这个工程:

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

这说明今年列表框只有2010:

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

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

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

他们也应该使用任何dateformat。

1

尝试使用的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" 
    }); 
});