【问题标题】:remove specific months from Datepicker从 Datepicker 中删除特定月份
【发布时间】:2019-01-24 08:16:30
【问题描述】:

在 wordpress 中,我试图删除 datepicker 中的特定月份,但我的代码不起作用。

    invalidMonths = [11,12,1,2];

function noInvalidMonths(date) {
    if(jQuery.inArray(date.getMonth()+1, invalidMonths)>-1){
        return [false, '']
    }
    return [true, ''];

}
jQuery(function($) {
    $("#date1").datepicker({
        minDate: 0,
        maxDate: null,
        dateFormat: 'MM dd, yy',
        beforeShowDay: noInvalidMonths
    });
});

【问题讨论】:

  • 什么都没有改变..

标签: jquery wordpress datepicker


【解决方案1】:

使用以下代码更改您的功能,

function noInvalidMonths(date) 
{
    var month = date.getMonth();
    if (jQuery.inArray(month, invalidMonths) != -1) {
         return [false];
     }
     return [true];    
  }

希望这会对你有所帮助。

【讨论】:

  • 请检查您的控制台,看看是否有任何 javascript 错误。如果有任何错误,请告诉我
  • 控制台没有错误,所有飞蛾都显示出来了。见在线链接:cantresformentera.com/book-now
  • 这是 contcatform 7 datepicker,您想扩展该字段,但您没有先添加适当的信息。上面的方法适用于日期选择器,它被称为给定的方式。联系表格 7 在其 js 中调用,因此您需要扩展它,
【解决方案2】:

像这样改变。注意代码中月份的增量,否则需要在invalidMonths数组中定义为'month-1'。

var invalidMonths = [11,12,1,2];

function noInvalidMonths(date) 
{
    var month = eval(date.getMonth()+1); //increment by 1 since the months start from 0

    if (jQuery.inArray(month, invalidMonths) != -1) {
         return [false];
     }
     return [true];    
}

jQuery(function($) {
    $("#date1").datepicker({
        minDate: 0,
        maxDate: null,
        dateFormat: 'MM dd, yy',
        beforeShowDay: noInvalidMonths
    });
});

更新: 停用您的“联系表 7 日期选择器”插件,将联系表中的“日期选择器”都更改为“文本”框。将以下内容添加到主题的“functions.php”中。

function wp_custom_enqueue_datepicker() {
    // Load the datepicker script (pre-registered in WordPress).
    wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) );
    wp_register_style('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css');
    wp_enqueue_style( 'jquery-ui' );   

    wp_add_inline_script( 'jquery-ui-datepicker', 'var invalidMonths = [11,12,1,2];  function noInvalidMonths(date)  {     var month = eval(date.getMonth()+1);   if (jQuery.inArray(month, invalidMonths) != -1) {          return [false];      }      return [true];     }  jQuery(function($) {     $("#date1,#date2").datepicker({         minDate: 0,         maxDate: null,         dateFormat: \'MM dd, yy\',         beforeShowDay: noInvalidMonths     });        var $date1 = $(\'#date1\'), $date2 = $(\'#date2\');     $date1.datepicker(\'option\', \'onSelect\', function(value){       $date2.datepicker(\'option\', \'minDate\', value);     }); });' );
}
add_action( 'wp_enqueue_scripts', 'wp_custom_enqueue_datepicker' );

【讨论】:

  • 将禁用指定月份的所有日期 - 使用此代码后显示的所有内容都启用了吗?
  • 只有指定月份的日期才会使用此代码禁用,月份仍会显示。如果没有,您可以在您的问题中发布屏幕截图吗?
  • 控制台没有错误,所有飞蛾都显示出来了。见在线链接:cantresformentera.com/book-now
  • datepicker 脚本已经在第 1269 行加载了元素名称 $('input[name="date-775"]').datepicker(),这就是为什么新脚本没有加载的原因。尝试将 beforeShowDay: noInvalidMonths 插入到初始脚本本身并删除第二个单独的脚本。
  • 检查答案中的“更新”部分,让我知道它是否有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
相关资源
最近更新 更多