在Month/DatePicker上禁用月份 [英] Disable Months On Month/DatePicker
问题描述
请查看我的小提琴.
我有一个月份选择器,它只允许用户提前选择一年,但是我想要的是过去几个月被禁用,以及一年之后的任何几个月都被禁用,但是我不知道如何将其转换为工作.
I have a monthpicker which only allows users to select a year in advance but what I want is for past months to be disabled and also any months after a year in advance to be disabled but I cant figure out how to get this to work.
示例场景 当前月份为"10月",因此对于"2015年"月份,"1月至9月"月份将被禁用,而对于"2016年年份",则禁用"11月至12月"月份.
Example Scenario Current month is 'October' so for 'Year 2015' months 'Jan to Sept' will be disabled and months 'Nov to Dec' will be disabled for 'Year 2016'
我尝试使用minDate:"0"和maxDate:"1y",但是它们不起作用.
I have tried using minDate: "0" and maxDate: "1y" but they don't work.
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')
});
推荐答案
DEMO
您可以使用startDate
和endDate
进行操作,但尝试在外部某处分配日期变量,如下所示:
You can do it with startDate
and endDate
but try assigning date variable somewhere outside as below:
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')
});
这篇关于在Month/DatePicker上禁用月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!