jQuery 限制 HTML 5 中的日期范围

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

Limiting date range in HTML 5

javascriptjqueryhtml

提问by Aravind Bharathy

I have a input field with type date

我有一个类型为日期的输入字段

<input type="date" id="datepick" name="mybirthday" placeholder="Birthdate(MM/DD/YYYY)" />

Here I want to limit the date range that the user can choose. To be more specific, I dont want the user to choose a birthdate like 22/08/2015. How can I do this?

这里我想限制用户可以选择的日期范围。更具体地说,我不希望用户选择像 22/08/2015 这样的生日。我怎样才能做到这一点?

回答by Matt Cain

Use the minand maxattributes:

使用minmax属性:

<label>Enter a date before 1989-10-30:</label>
<input type="date" name="myDate" max="1989-10-29">

<label>Enter a date after 2001-01-01:</label>
<input type="date" name="myDate" min="2001-01-02">

回答by Subliminal Hash

I am not sure about that but I remember reading somewhere that HTML5 datepickers are based on browser locale and might not give you desired output in all your visitors' browsers. E.g. in the US the date format is MM/dd/yyyy whereas in the UK it is dd/MM/yyyy.

我不确定这一点,但我记得在某处读到过 HTML5 日期选择器基于浏览器区域设置,可能无法在所有访问者的浏览器中为您提供所需的输出。例如,在美国,日期格式为 MM/dd/yyyy,而在英国则为 dd/MM/yyyy。

I didn't want to deal with all that or leave it to luck so I ended up using jQueryUI datepicker.. Many events are already readily available for you there...

我不想处理所有这些,也不想靠运气,所以我最终使用了 jQueryUI 日期选择器。