twitter-bootstrap 动态重新初始化或销毁 Bootstrap 日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23582366/
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 20:41:45  来源:igfitidea点击:

Re-initialize or destroy Bootstrap datepicker dynamically

twitter-bootstrapmethodsdatepickerdestroy

提问by dan

Is there a way to destroy the Bootstrap datepicker dynamically updating its options like format, beforeShowDay, etc.? I know that the jQuery UI datepicker has a destroy method but Bootstrap's has not. It only has the .('remove')method but its not working.

有没有办法破坏 Bootstrap 日期选择器动态更新其选项,如格式、beforeShowDay 等?我知道 jQuery UI datepicker 有一个 destroy 方法,但 Bootstrap 没有。它只有.('remove')方法,但不起作用。

My goal is to modify the options of the datepicker when changing an input, for example:

我的目标是在更改输入时修改日期选择器的选项,例如:

$('#employee').change( function() {
   $('#date').datepicker('destroy'); //not working 'cause bootstrap hasnt a destroy method
   $('#date').hide();
});

Then I call the initialize function when the input changes:

然后我在输入改变时调用初始化函数:

function updateCalendar(){
     $('#date').datepicker({
          format:"dd/mm/yyyy",
          beforeShowDay: updateC  //function that modifies the available days to show
     });    
 }

回答by Rajesh Madhu

$('.datepicker').datepicker('remove');

Make sure you have your date picker object in DOM before removing it. Without removing you can hide the calendar and change the format of date and update it .

在删除它之前,请确保在 DOM 中有你的日期选择器对象。在不删除的情况下,您可以隐藏日历并更改日期格式并更新它。

$('.datepicker').datepicker('update');

回答by Anirban

You can Use:

您可以使用:

$('#element_id').datepicker('destroy');

$('#element_id').datepicker('destroy');

I called this just outside my ajax call. My ajax call was creating the datepicker dynamically. After the call was made I destroyed the object.

我在我的 ajax 调用之外调用了这个。我的 ajax 调用是动态创建日期选择器的。调用完成后,我销毁了该对象。

Like below:

像下面这样:

$.ajax({ url: 'my_ business_functions.php',
     data: {func_call: 'my_method_name', my_parameter: my_parameters},
     type: 'post',
     success: function(output) {
         //all my stuffs here
        $('#element_id').val(datepicker_value);
        }
    });
    $('#element_id').datepicker('destroy');

This served my purpose.

这达到了我的目的。

回答by rnirnber

I actually found it more to just remove the element's ID, clone it, and add the ID back in.

我实际上发现更多的是删除元素的 ID,克隆它,然后重新添加 ID。

回答by KnowGe

Propably you are using datepicker with input btn group, I mean you with a date icon button. So, you should not choose just input element when you want to destroy it and reinit. You should select div has "date-picker", then remove and init again.

可能您正在使用带有输入 btn 组的 datepicker,我的意思是您带有日期图标按钮。所以,当你想销毁它并重新初始化时,你不应该只选择输入元素。您应该选择 div 具有“日期选择器”,然后再次删除并初始化。

回答by Ato Thejay

forceParse: 1

In your options. It will parse the input date field value every time.

在您的选择中。每次都会解析输入的日期字段值。