jQuery UI datepicker:限制某些月份(而不是几个月的范围) [英] jQuery UI datepicker: limit certain months (not a range of months)

查看:209
本文介绍了jQuery UI datepicker:限制某些月份(而不是几个月的范围)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望限制jQuery UI datepicker的月份.

例如,我要阻止用户在任何给定年份中选择4月,8月和10月.我的月份有一系列数字,例如4,8,10.

For example, I want to prevent users from selecting april, august and October in any given year. I have my months in an array of numbers, eg 4,8,10.

推荐答案

我希望您尝试了一些操作,但是我有一些空闲时间,因此,如果我正确地理解您想停用几个月的功能,就可以了.

I hope you tried something, but I have a few spare minutes so f I understand correctly you want to disable the use of some months.

您可以使用jQuery UI Datepicker的 beforeShowDay 事件并检查是否每天是否处于活动状态(根据其月份),并检查是否在数组中.

You can use the beforeShowDay event of jQuery UI Datepicker and check if a day is active or not based on its month and check if is in the array.

要检查数组,我使用jQuery inArray 方法.

To check the array I use the jQuery inArray method.

默认值:null一个将日期作为参数并且必须 返回以下数组:[0]:true/false,指示是否 日期是可选的 1 :要添加到日期单元格的CSS类名称,或者 默认呈现 2 的":可选的弹出式工具提示 此日期每天之前的日期选择器中调用该函数 它会显示出来.

Default: null A function that takes a date as a parameter and must return an array with: [0]: true/false indicating whether or not this date is selectable 1: a CSS class name to add to the date's cell or "" for the default presentation 2: an optional popup tooltip for this date The function is called for each day in the datepicker before it is displayed.

jQuery.inArray

描述:在数组中搜索指定值并返回 其索引(如果找不到,则为-1).

Description: Search for a specified value within an array and return its index (or -1 if not found).

代码示例:

monthArray = [0, 3, 6]

$(function () {
    $("#datepicker").datepicker({
        beforeShowDay: function (date) {
            //getMonth()  is 0 based
            if ($.inArray(date.getMonth(), monthArray) > -1) {
                return [false, ''];
            }
            return [true, ''];
        }
    });
});

这是一个有效的小提琴: http://jsfiddle.net/APP5N/

Here is a working fiddle: http://jsfiddle.net/APP5N/

这篇关于jQuery UI datepicker:限制某些月份(而不是几个月的范围)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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