【问题标题】:How to disable today's date in jQuery DatePicker (Wordpress)如何在 jQuery DatePicker (Wordpress) 中禁用今天的日期
【发布时间】:2017-08-21 20:07:06
【问题描述】:

我目前正在使用一个名为 Flexible Checkout Fields Pro 的插件。我想调整日期选择器上的设置。我们公司为入住酒店和游轮的游客提供订单送货服务。唯一的问题是我们很少在下订单的同一天发货,周末也不发货。我联系了开发人员并获得了本指南以查看: http://www.spiceforms.com/blog/how-to-disable-dates-in-jquery-datepicker-a-short-guide/

这里它给了我“noWeekends”的编码:

$(function() {
    $( "#datepicker" ).datepicker({
        beforeShowDay: $.datepicker.noWeekends
    });
});

我不知道在我的 WordPress 目录中的何处插入此代码,并且我仍然没有任何关于如何在时间选择器上禁用当前日期的信息...任何帮助将不胜感激。

【问题讨论】:

    标签: javascript jquery wordpress datepicker


    【解决方案1】:

    您可以将以下内容粘贴到您的页脚中,它应该可以工作。否则可以去掉<script></script>标签,粘贴到外部JS文件中,只要插件加载后调用即可。

    让我为你分解一下:

    阅读整个代码中的 cmets

    <script>
    
    // First we have to enable the jQuery '$' selector in WP.
    (function($) {
    
    /** Days to be disabled as an array */
    var disableddates = [];
    
    function DisableSpecificDates(date) {
    
        // Get the current date values
        var m = date.getMonth(); // Month
        var d = date.getDate(); // Day
        var y = date.getFullYear(); // Year
    
        // Convert the date in to the mm-dd-yyyy format
        var currentdate = m + '-' + d + '-' + y ;
    
        // Add the current date to the disableddates array
        disableddates.push(currentdate);
    
        // We will now check if the date belongs to disableddates array
        for (var i = 0; i < disableddates.length; i++) {
    
            // Now check if the current date is in disabled dates array.
            if ($.inArray(currentdate, disableddates) != -1 ) {
                return [false];
            }
        }
    
        // In case the date is not present in disabled array, we will now check if it is a weekend.
        // We will use the noWeekends function
        var weekenddate = $.datepicker.noWeekends(date);
        return weekenddate;
    
    }
    
    // Initialize the datepicker function and call the 'DisableSpecificDates' as the 'beforeShowDay' callback.
    $( "#datepicker" ).datepicker({
        beforeShowDay: DisableSpecificDates
    });
    
    })(jQuery); // Close out WP jQuery.
    </script>
    

    【讨论】:

    • 您好,谢谢。不过还是有问题。我将代码粘贴到页脚中,但什么也没发生,所以我创建了一个没有脚本标签的外部 JS 文件,并将此代码加载到我的 functions.php 目录中: add_action('wp_enqueue_scripts', 'add_my_script'); function add_my_script() { wp_enqueue_script( 'noweekends', get_stylesheet_directory_uri() . '/DockyardGlassWorks/wp-content/themes/x-child/js/noweekends.js' 数组('jquery') ); wp_enqueue_script('子主题脚本');现在我收到了 HTTP 500 错误。
    • 如果您使用的是get_stylesheet_directory_uri(),则无需添加完整路径。它应该只是get_stylesheet_directory_uri() . '/js/noweekends.js' 或者你可以使用get_template_directory_uri() . '/js/noweekends.js'
    • 是的,我也试过了——但仍然出现这个错误。没关系,我暂时把它放在次要位置。感谢您的帮助。
    猜你喜欢
    • 2011-10-24
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多