显示了jQuery datepicker年

Joh*_*ker 78 jquery datepicker

使用jQuery datepicker,如何更改显示的年份范围?在jQuery UI网站上,它表示默认为"显示当前年份之前和之后的10年".我想用这个作为生日选择,而今天10年之前是不行的.这可以使用jQuery datepicker完成,还是我必须使用不同的解决方案?

链接到datepicker演示:http://jqueryui.com/demos/datepicker/#dropdown-month-year

Sho*_*og9 165

如果你向下看一下演示页面,你会看到一个"限制日期选择器"部分.使用下拉列表指定" Year dropdown shows last 20 years"演示,然后点击查看源:

$("#restricting").datepicker({ 
    yearRange: "-20:+0", // this is the option you're looking for
    showOn: "both", 
    buttonImage: "templates/images/calendar.gif", 
    buttonImageOnly: true 
});
Run Code Online (Sandbox Code Playgroud)

你会想这样做(显然改变-20-100什么的).

  • 你也可以使用`c`而不是`+ 0`,所以`yearRange:" - 20:c",` (5认同)

Pli*_*pie 40

为什么不显示年份或月份选择框?

$( ".datefield" ).datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange:'-90:+0'
});
Run Code Online (Sandbox Code Playgroud)


War*_*ent 10

没有人提出的是你还可以设置硬编码的日期范围:

例如:

yearRange: "1901:2012"
Run Code Online (Sandbox Code Playgroud)

虽然不建议不这样做,但它是一个完全有效的选项(如果您合法地寻找目录中的特定年份 - 例如"1963:1984"),则该选项很有用.


Mav*_*ick 6

完美的出生日期(以及我使用的)类似于Shog9所说的,尽管我将给出一个更具体的DOB示例:

$(".datePickerDOB").datepicker({ 
    yearRange: "-122:-18", //18 years or older up to 122yo (oldest person ever, can be sensibly set to something much smaller in most cases)
    maxDate: "-18Y", //Will only allow the selection of dates more than 18 years ago, useful if you need to restrict this
    minDate: "-122Y"
});
Run Code Online (Sandbox Code Playgroud)

希望未来的googlers发现这个有用:).


Jam*_*rue 5

添加到@ Shog9发布的内容,您还可以在beforeShowDay:callback函数中单独限制日期.

您提供了一个获取日期并返回布尔数组的函数:

"$(".selector").datepicker({ beforeShowDay: nationalDays}) 
natDays = [[1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'], [4, 27, 'za'], 
[5, 25, 'ar'], [6, 6, 'se'], [7, 4, 'us'], [8, 17, 'id'], [9, 7, 
'br'], [10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']]; 
function nationalDays(date) { 
    for (i = 0; i < natDays.length; i++) { 
      if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == 
natDays[i][1]) { 
        return [false, natDays[i][2] + '_day']; 
      } 
    } 
  return [true, '']; 
} 
Run Code Online (Sandbox Code Playgroud)