twitter-bootstrap 将年龄限制设为 18 岁 - Bootstrap Datepicker
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21880917/
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
Put age restriction of 18 years - Bootstrap Datepicker
提问by Hassan Sardar
I am using Bootstrap Datepicker.
我正在使用 Bootstrap 日期选择器。
I want to put an Age Restriction of 18 Years.
我想设置 18 岁的年龄限制。
Dates less than Age 18 Years from current date should be disabled.
应禁用距当前日期小于 18 岁的日期。
Here is my Fiddle:
这是我的小提琴:
JS:
JS:
$(function()
{
$('#datepicker').datepicker(
{
viewMode: "years",
defaultDate: '-2yr',
}
);
});
回答by sparsh turkane
Simply put endDate: '-18y' if you only want to allow user whose age is 18 years and above.
如果您只想允许年龄为 18 岁及以上的用户,只需输入 endDate: '-18y' 。
<input type="text" class="datepicker">
<script>
$('.datepicker').datepicker({
endDate: '-18y'
});
</script>
Working Example: https://jsfiddle.net/sparshturkane/w46euf5q/1/
工作示例:https: //jsfiddle.net/sparshturkane/w46euf5q/1/
Please refer Bootstrap Date picker docsfor more options and instructions
有关更多选项和说明,请参阅Bootstrap 日期选择器文档
回答by Lee Bailey
This is the JavaScript you are after:
这是您所追求的 JavaScript:
var dt = new Date();
dt.setFullYear(new Date().getFullYear()-18);
$('#datepicker').datepicker(
{
viewMode: "years",
endDate : dt
}
);
But your fiddle is using coffeescript so here is the same in coffee:
但是你的小提琴使用的是咖啡脚本,所以这里的咖啡是一样的:
dt = new Date()
dt.setFullYear new Date().getFullYear() - 18
$("#datepicker").datepicker
viewMode: "years"
endDate: dt
working example: http://jsfiddle.net/Ln5x6/1/
工作示例:http: //jsfiddle.net/Ln5x6/1/
回答by mukesh
You can use yearRange, to let users choose only years which comes under 18+ from current year. Like,
您可以使用 yearRange,让用户只选择从当年起 18 岁以上的年份。喜欢,
jQuery('#dateob').datepicker({
changeMonth: true,
changeYear: true,
yearRange: '-110:-18',
showButtonPanel: true
});

