【问题标题】:How can i disable past weekends in Jquery datepicker?如何在 Jquery datepicker 中禁用过去的周末?
【发布时间】:2014-01-30 23:11:29
【问题描述】:

我有一个只显示周末可供选择的日历... 但我想禁用过去的周末...... 示例:现在是第 30 天,但日历显示今天日期之前的周末...

Example here

以及如何添加节日?

代码如下:

jQuery(document).bind('gform_post_render', function(){
        jQuery(".datepicker").datepicker('destroy').datepicker({
            showOn: "both",
            buttonImage: "/wp-content/plugins/gravityforms/images/calendar.png",
            buttonImageOnly: true,
            dateFormat: "mm/dd/yy",
            firstDay: 1,
            currentText: 'Hoy',
            monthNames: ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio',
'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
            monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun',
'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
            dayNames: ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'],
            dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mié;', 'Juv', 'Vie', 'Sáb'],
            dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sá'],
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: '',
            beforeShowDay: function(date) {
                var day = date.getDay();
                return [(day == 0 || day == 6)];
            }
        });
    });

提前非常感谢!

【问题讨论】:

    标签: jquery calendar datepicker


    【解决方案1】:

    对于禁用节日日期,我认为您必须添加一个包含这些日期的数组,也许您可​​以将它们从数据库中提取或硬编码,这是一个示例:

    var festiveDays = ["15-2-2014","23-2-2014","2-3-2014"];
    

    至于仅显示“今天”之后的日期,只需将 minDate: 0 添加到日期选择器即可。现在如果你想将它们两者结合起来,这是最终的结果:

    // Festive days dates (lets use these three days as example)
    var festiveDays = ["15-2-2014","23-2-2014","2-3-2014"];
    
    // Initialize datepicker
    $("#datepicker").datepicker({
        firstDay: 1,
        minDate: 0,
        beforeShowDay: function(dt) {
            var dates = dt.getDate() + "-" + (dt.getMonth()+1) + "-" + dt.getFullYear();
            if ($.inArray(dates, festiveDays) < 0) {
                return [dt.getDay() == 0 || dt.getDay() == 6, ""];
            } else {
                return [false,"",""];
            }
        }
    });
    

    这是fiddle,您可以对其进行测试。

    【讨论】:

    • 您好 Tomatrox!非常感谢您的回答...我已经尝试过“minDate:0”并且效果很好!但是如果我尝试“var day = date.getDay(); return [(day == 0 || day == 6)];”在我上面放的代码之前,因为有 javascript 冲突所以不工作......我该如何解决它?提前致谢!
    • 你能更新你的代码吗?或者将它上传到 jsfiddle 上?所以我可以看到错误在哪里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多