twitter-bootstrap 如何重新初始化引导日期选择器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27297059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 21:57:57  来源:igfitidea点击:

How can I reinitialize a bootstrap datepicker?

javascriptjquerytwitter-bootstrapdatepicker

提问by RatTub

A bootstrap datepicker is initialized on load. How can I change the options for that datepicker afterthe page is loaded and the datepicker has already been initialized?

加载时初始化引导日期选择器。在页面加载并且日期选择器已经初始化,如何更改该日期选择器的选项?

See here for an example http://jsfiddle.net/Leoqs1xg/

参见这里的示例http://jsfiddle.net/Leoqs1xg/

The weekStart is set to 4 (Thursday) at first with

最初将 weekStart 设置为 4(星期四)

$('#mydate').datepicker({weekStart: 4});

but if I change it to something else with

但如果我把它改成别的东西

$('#mydate').datepicker({weekStart: 1});

weekStart is still 4.

weekStart 仍然是 4。

采纳答案by strapro

I think you need to remove and re-initialize the object

我认为您需要删除并重新初始化对象

More info here

更多信息在这里

Bootstrap Datepicker re-initialize date format

Bootstrap Datepicker 重新初始化日期格式

回答by DonJoe

There is now a method destroyavailable.

现在有一种方法destroy可用。

I would do it like this:

我会这样做:

$('#sv_pickup_date').datepicker('destroy').datepicker({
    format: 'dd/mm/yyyy'
    , autoclose: true
    , startDate: '20/08/2018'
    , endDate: '24/08/2018'
});

Optionally you may want to also use clearDatesmethod.

(可选)您可能还想使用clearDates方法。

Like this you can define your own reinitializemethod for bootstap datepicker.

像这样,您可以reinitialize为 bootstap datepicker定义自己的方法。

回答by Matija

I faced the same hurdle in my current project where on change of dropdown I recreate and recheck the available dates. So for that purpose I needed to send over ajax new enabled dates. @strapro was on the right track.

我在当前项目中遇到了同样的障碍,在更改下拉菜单时,我重新创建并重新检查可用日期。因此,为此我需要通过 ajax 发送新的启用日期。@strapro 走在正确的轨道上。

if (called_from_type_of_retreat == 'IGR') {
    var myMinDateToday = getTodaysDate();
    var enabledDatesArrayIGR = '';
    $('#arrival_date_div_IGR').datetimepicker();
    /*Ajax call for IGR*/
    $.ajax({
        type: "get",
        url: "getAjaxAvailableDatesIGR",
        data: {
        "todays_date": myMinDateToday
        },
        dataType: "json",
        success: function(data) {
            /*Assign array which is returned as Json*/
            enabledDatesArrayIGR = data.availableDatesArray;
            /* DateTime Pickers */
            $('#arrival_date_div_IGR').datetimepicker('destroy');
            $('#arrival_date_div_IGR').datetimepicker({
                format: "DD.MM.YYYY",
                ignoreReadonly: true,
                enabledDates: enabledDatesArrayIGR
            });
        },
        error: function(xhr, status, errorThrown) {
            console.log(xhr.responseText);
        }
    });
}

Pay attention to raw initialize of datetimepicker lines 4.

注意日期时间选择器行 4 的原始初始化。

Then in Ajax call I destroy that datetimepicker (line 17) of datetimepicker and set my settings line (18-22).

然后在 Ajax 调用中,我销毁 datetimepicker 的那个 datetimepicker(第 17 行)并设置我的设置行(18-22)。

My version of eonasdan bootstrap datetimepickeris : 4.17.45

我的eonasdan bootstrap datetimepicker版本是:4.17.45

回答by Plippie

There is no re-initialization. However if you update the value of the input programmatically, for example another part of your code, and want to see the change when you open the picker you could use setDate to read the new value.

没有重新初始化。但是,如果您以编程方式更新输入的值,例如代码的另一部分,并希望在打开选择器时查看更改,则可以使用 setDate 读取新值。

// var mynewdate = your-converted-to-date-string;
$('.datepicker').datepicker('setDate', mynewdate);