在 Month/DatePicker 上禁用月份

Disable Months On Month/DatePicker

请看我的fiddle.

我有一个 monthpicker 只允许用户 select 提前一年,但我想要的是禁用过去几个月以及提前一年后的任何月份,但我想不通了解如何让它发挥作用。

示例场景 当前月份是 'October',因此 'Year 2015' 个月 'Jan to Sept' 将被禁用,'Nov to Dec' 个月将被禁用 'Year 2016'

我试过使用 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')
});

DEMO

您可以使用 startDateendDate 来完成,但请尝试在外部某处分配日期变量,如下所示:

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')
});

what I want is for past months to be disabled and also any months after a year in advance to be disabled

I have tried using minDate: "0" and maxDate: "1y" but they don't work.

你走在正确的轨道上。但是,使用 startDateendDate 代替 minDatemaxDate。像这样:

$('#example1').datepicker ({
    startDate: "-0m",
    endDate: "+1y",    
    ...
});

-0m 最多只允许这个月,+1y 最多只允许一年。

Fiddle: http://jsfiddle.net/abhitalks/RWY2X/34/

使用它来禁用之前和未来的月份并启用当前月份和日期。

<script type="text/javascript">
$(document).ready(function () {
    var date = new Date();
    //Create Variable for first day of current month
    var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
    $("#txtDate").datepicker({
        endDate: new Date(),
        startDate: firstDay,
    });
});
</script>