如何使用beforeShowDay方法在每个datepicker范围内禁用不同的月份 [英] How to disable different months in each of the datepicker range years using beforeShowDay method

查看:415
本文介绍了如何使用beforeShowDay方法在每个datepicker范围内禁用不同的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在开始年份和结束年份的每个日期选择年份中禁用不同的月份。我知道我可以使用 beforeShowDay 方法来运行一些可以返回true和false的代码来启用/禁用日期。现在我想禁用2005年9月 - 12月和2009年11月+ 12月的时间,但我不完全确定如何做到这一点。

I would like to disable different months in each of of datepicker years specifically the start year and the end year. I know I can use the beforeShowDay method to run some code that can return true and false to enable/disable dates. Right now I want to disable months Sept - Dec in 2005 and Nov + Dec in 2009 but I'm not completely sure how to do this.

当我退出 date.getYear()当我希望得到2014年和2015年等等,所以我不知道我如何检查一年,然后继续,并根据我创建的最小和最大数组,将保留我想排除的几个月,设置true / false值。

When I log out date.getYear() I get numbers like 104 an 105 when I would expect to get 2014 and 2015 etc so I'm not sure how I can check the year to then go ahead and set the true/false values based on the min and max arrays I have created that will hold the months I want to exclude.

JS

var minYear = 2005,
    maxYear = 2009,
    excludeMinYearMonths = [8, 9, 10, 11],
    excludeMaxYearMonths = [10, 11];

    $("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: minYear + ':' + maxYear,
        beforeShowDay: function (date) {

            console.log(date.getYear());

            if (date.getYear() === minYear) {
                if ($.inArray(date.getMonth(), excludeMinYearMonths) > -1) {
                    return [false, ''];
                }
                return [true, ''];
            }

            if (date.getYear() === maxYear) {
                if ($.inArray(date.getMonth(), excludeMaxYearMonths) > -1) {
                    return [false, ''];
                }
                return [true, ''];
            }

        },
        defaultDate: '01/01/05'
    });

Jsfiddle: http://jsfiddle.net/qA9NT/18/

推荐答案

您可以先阅读的文档 getYear() 。在那里你会看到为什么 使用getFullYear()

You can start by reading a documentation of getYear(). There you will see why getFullYear() is used instead.


getYear() 方法将
的指定日期的年份减去1900到当地时间。因为 getYear() 不会返回全年,它不再使用,而已被
getFullYear() 方法。

The getYear() method returns the year minus 1900 in the specified date according to local time. Because getYear() does not return full years, it is no longer used and has been replaced by the getFullYear() method.

另外,你忘了定义一个非排除日期的默认行为。

Also, you forgot to define a default behavior for the non-excluded dates.

var minYear = 2005, maxYear = 2009, excludedMonths = {
    2005: [8, 9, 10, 11],
    2009: [10, 11]
};

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: minYear + ':' + maxYear,
    beforeShowDay: function (date) {
        if (excludedMonths.hasOwnProperty(date.getFullYear())) {
            if ($.inArray(date.getMonth(), excludedMonths[date.getFullYear()]) > -1) {
                return [false, ''];
            }
        }
        return [true, ''];
    },
    defaultDate: '01/01/05'
});

检查更新的实例。

这篇关于如何使用beforeShowDay方法在每个datepicker范围内禁用不同的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆