请看我的fiddle .
我有一个 monthpicker,它只允许用户提前选择一年,但我想要的是禁用过去几个月以及提前一年之后的任何月份,但我无法弄清楚如何做到这一点工作。
示例场景 当前月份是“10 月”,因此对于“2015 年”月份“1 月至 9 月”将被禁用,而“2016 年”月份“11 月至 12 月”将被禁用
我试过使用 minDate: "0"和 maxDate: "1y"但它们不起作用。
HTML
<div class="input-group date" style="width: 200px">
<input type="text" id="example1" class="form-control" style="cursor: pointer"/>
<span class="input-group-addon">
<i class="glyphicon glyphicon-calendar"></i>
</span>
</div>
JQuery
$('#example1').datepicker
({
format: "MM yyyy",
minViewMode: 1,
autoclose: true,
startDate: new Date(new Date().getFullYear(), '0', '01'),
endDate: new Date(new Date().getFullYear()+1, '11', '31')
});
最佳答案
您可以使用 startDate
和 endDate
来完成,但请尝试在外部某处分配日期变量,如下所示:
var date=new Date();
var year=date.getFullYear(); //get year
var month=date.getMonth(); //get month
$('#example1').datepicker
({
format: "MM yyyy",
minViewMode: 1,
autoclose: true,
startDate: new Date(year, month, '01'), //set it here
endDate: new Date(year+1, month, '31')
});
关于javascript - 在月/日期选择器上禁用月份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32945935/