twitter-bootstrap 在引导日期选择器中设置开始日期限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15075984/
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
setting start date limit in bootstrap datepicker
提问by Michael
Update:
更新:
Updated date format in HTML and assigned today's date to data, although date range limit not restricting.
更新了 HTML 中的日期格式并将今天的日期分配给数据,尽管日期范围限制没有限制。
New fiddle: http://jsfiddle.net/D9Xav/143/
新小提琴:http: //jsfiddle.net/D9Xav/143/
==========
==========
I'm trying to a create a datepicker which allows the user to select a year and month. Currently, I'm confused as to why the start month is 2013, and why the start date limit is not preventing me to go beyond x year?
我正在尝试创建一个允许用户选择年份和月份的日期选择器。目前,我很困惑为什么开始月份是 2013 年,为什么开始日期限制没有阻止我超过 x 年?
Here's the code: http://jsfiddle.net/D9Xav/140/
这是代码:http: //jsfiddle.net/D9Xav/140/
Docs:
文档:
Code here because rules:
在这里编码,因为规则:
<div id="dpMonths" class="input-append date" data-date-minviewmode="months"
data-date-viewmode="years" data-date-format="mm-yyyy"
>
<input type="text" readonly="">
<span class="add-on">
<i class="icon-th"></i>
</span>
</div>
$('#dpMonths').data({
date: '01/01/2000'
});
$('#dpMonths').datepicker('update');
$('#dpMonths').datepicker({
startDate: '+10y'
}).on('changeDate', function (ev) {
console.log(ev.date);
});
I set the datepicker data, because it'll break otherwise, wasn't sure how to get round it/nor do I know why it get's overwritten with today's date/month.
我设置了日期选择器数据,因为否则它会损坏,不知道如何绕过它/我也不知道为什么它会被今天的日期/月份覆盖。
回答by frostyterrier
It looks like the date value 01/01/2000 needs to be in the same format as the date format you chose: mm-yyyy. So it needs to be 01-2000.
看起来日期值 01/01/2000 需要与您选择的日期格式相同:mm-yyyy。所以它需要是01-2000。
$('#dpMonths').data({
date: '01-2000'
});
You could add that value into your HTML as a data attribute instead though, e.g. data-date="01-2000"
不过,您可以将该值作为数据属性添加到 HTML 中,例如 data-date="01-2000"
Once you make that change, the date limit will also work.
进行更改后,日期限制也将起作用。
[edit] After you've updated to the new version, change your javascript part to just this:
[编辑] 更新到新版本后,将 javascript 部分更改为:
$('#dpMonths')
.datepicker({
startDate: '-3y',
format: 'mm-yyyy'
})
.on('changeDate', function (ev) {
console.log(ev.date);
});
Fiddle: http://jsfiddle.net/SdZBF/5/

