代码之家  ›  专栏  ›  技术社区  ›  John Boker

显示jQuery日期选择器年份

  •  78
  • John Boker  · 技术社区  · 15 年前

    使用jQuery日期选择器,如何更改显示的年份范围?在jQueryUI站点上,它说默认值是“显示当前年份之前和之后的10年”。我想用这个来选择生日,10年前的今天是不行的。这可以通过jQuery日期选择器来完成,还是必须使用不同的解决方案?

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

    8 回复  |  直到 12 年前
        1
  •  165
  •   Shog9    15 年前

    如果你向下看一下演示页面,你会看到一个“限制日期选择器”部分。使用下拉列表指定“ 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 
    });
    

    -20 -100

        2
  •  40
  •   Plippie    10 年前

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

    $( ".datefield" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange:'-90:+0'
    });
    
        3
  •  10
  •   Warren Sergent    12 年前

    其他人没有提到的是,您还可以设置硬编码的日期范围:

    yearRange: "1901:2012"
    

    虽然不这样做可能是明智的,但是,这是一个完全有效的选项(如果您合法地在目录中查找某一特定年份,例如“1963:1984”,则该选项非常有用)。

        4
  •  6
  •   SEsterbauer    8 年前

    完美的出生日期字段(以及我使用的)类似于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"
    });
    

    希望未来的谷歌用户会发现这一点有用:)。

        5
  •  5
  •   JamesSugrue    15 年前

    提供一个函数,该函数接受日期并返回布尔数组:

    "$(".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, '']; 
    } 
    
        6
  •  3
  •   Manish    10 年前
     $("#DateOfBirth").datepicker({
            yearRange: "-100:+0",
            changeMonth: true,
            changeYear: true,
        });
    

    yearRange:'1950:2013',//指定硬编码的年份范围 还是这样

    它将有助于显示年度和月份选择的下拉列表。

        7
  •  2
  •   sth Wojciech Parzych    15 年前

    au、nz、ie等是显示其国庆日的国家的国家代码(澳大利亚、新西兰、爱尔兰等)。如代码中所示,这些值与“_day”组合在一起,并作为CSS样式传递回当天应用。相应的样式如下所示,它将当天的文本移到一边,并将其替换为国旗图像。

    .au_day {
      text-indent: -9999px;
      background: #eee url(au.gif) no-repeat center;
    }
    

        8
  •  1
  •   Gwenc37 Anand Shukla    10 年前

    $(function () {
        $(".DatepickerInputdob").datepicker({
            dateFormat: "d M yy",
            changeMonth: true,
            changeYear: true,
            yearRange: '1900:+0',
            defaultDate: '01 JAN 1900'
        });