jQuery 生日文本框的Datepicker插件

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

Datepicker plugin for birth date textbox

jqueryjquery-pluginsdatepicker

提问by Henry Gunawan

I've found a very easy to use datepicker called jquery datepicker. But the problem is when I want to use it for birth date. For example: my birth year is 1986 and the current year is 2013, then I need to click left arrow many times to decrease the months until I find 1986. It will be more troublesome if my birth year is 1960 or below.

我发现了一个非常易于使用的日期选择器,称为 jquery datepicker。但问题是当我想将它用于出生日期时。例如:我的出生年份是1986年,当前年份是2013年,那么我需要多次点击左箭头来减少月份,直到找到1986年。如果我的出生年份是1960年或以下,那就更麻烦了。

Does anybody know any datepicker plugin that suitable for this? Perhaps a datepicker which year or month can be selected from dropbox or something?

有没有人知道任何适合于此的日期选择器插件?也许日期选择器可以从 Dropbox 或其他东西中选择哪一年或哪一个月?

回答by Yusubov

In short:you may do that bythe Restricting Datepicker section in the demo page.

简而言之:您可以通过演示页面中的限制日期选择器部分来执行此操作。

If you look down the demo pagea bit, you'll see a "Restricting Datepicker" section. Use the dropdown to specify the "Year dropdown shows last 20 years" demo , and hit view source:

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

You'll want to do the same (obviously changing -20to -100or something).

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

Option # 2:is using the changeMonthand changeYearoptions of the api.

选项#2:使用api的changeMonthchangeYear选项。

Try using 'yy' for the current year like:

尝试使用 'yy' 表示当前年份,例如:

$('.datepicker').datepicker({changeYear: true, yearRange : 'yy-50:yy+1'});

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

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

回答by Casey Crookston

I was having the same issue, so in case someone else comes along with the same problem, I thought I'd post the solution. It's in the answer above, but you have to read the comments to get all of it.

我遇到了同样的问题,所以如果其他人遇到同样的问题,我想我会发布解决方案。它在上面的答案中,但您必须阅读评论才能获得所有内容。

In the head:

在头部:

<script src="~/Scripts/jquery-1.10.2.js"></script>

<script>
    $(function () {
        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            yearRange: '-100:+0'
        });
    });
</script>

And in the HTML:

在 HTML 中:

<input type="text" class="form-control" id="datepicker">

This will get you a date picker where the month is it's own dropdown, the year is it's own dropdown, and the year starts at today's year and goes back 100 years.

这将为您提供一个日期选择器,其中月份是它自己的下拉列表,年份是它自己的下拉列表,而年份从今天的年份开始并可以追溯到 100 年前。

回答by Kapil Seth

Add below line in datepicker call for the range you want to show in dropdown

在 datepicker 调用中为要在下拉列表中显示的范围添加以下行

yearRange: "1930:2025"

like this,

像这样,

$( function() {
    $( "#celeb_dob" ).datepicker({
      changeMonth: true,
      changeYear: true,
      yearRange: "1930:2025"
    });
  } );