初始化后设置选项时,datetimepicker 中的 disabledTimeIntervals 不起作用

disabledTimeIntervals in datetimepicker not worked when set the option after initialize

我使用 bootstrap datetimepicker 来构建我的日期和时间选择器。

我在页面初始化时按如下方式设置时间选择器:

$('#PickupTime').datetimepicker({
    locale: 'en',
    format: 'hh:mm A',
    stepping: 30,
});

我想根据集成的数据 return 设置时间选择器的最小和最大时间。

$.ajax({
    url: url,
    data: data,
    method: 'post',
    success: function (result) {
        debugger;
        $('#PickupTime').datetimepicker({
            disabledTimeIntervals: [[moment().hour(0), moment().hour(8).minutes(30)], [moment().hour(20).minutes(30), moment().hour(24)]],               
        });    

    },
});

但我注意到 disabledTimeIntervals 没有用, disabledTimeIntervals 仅在我将其放入初始化函数时才有效。

您必须使用 disabledTimeIntervals 函数而不是 disabledTimeIntervals 选项。

请注意,正如 docs 所说:

All functions are accessed via the data attribute e.g. $('#datetimepicker').data("DateTimePicker").FUNCTION()

您的代码将是:

$.ajax({
    url: url,
    data: data,
    method: 'post',
    success: function (result) {
        debugger;
        $('#PickupTime').data("DateTimePicker").disabledTimeIntervals([
            [moment().hour(0), moment().hour(8).minutes(30)],
            [moment().hour(20).minutes(30), moment().hour(24)]             
        ]});
    },
});