Clear the value of bootstrap-datepicker

前端 未结 10 1880
甜味超标
甜味超标 2020-12-28 12:56

I am using bootstrap-datepicker from here: https://github.com/eternicode/bootstrap-datepicker

version: 2.3.2

I am having trouble to clear the date values of

相关标签:
10条回答
  • 2020-12-28 13:27

    I was having similar trouble and the following worked for me:

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

    Both method calls were needed, otherwise it didn't clear.

    0 讨论(0)
  • 2020-12-28 13:28

    You can use this syntax to reset your bootstrap datepicker

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

    reference http://bootstrap-datepicker.readthedocs.org/en/latest/methods.html#update

    0 讨论(0)
  • 2020-12-28 13:29

    I came across this thread while trying to figure out why the dates weren't being cleared in IE7/IE8.
    It has to do with the fact that IE8 and older require a second parameter for the Array.prototype.splice() method. Here's the original code in bootstrap.datepicker.js:

    clear: function(){
        this.splice(0);
    },
    

    Adding the second parameter resolved my issue:

    clear: function(){
        this.splice(0,this.length);
    },
    
    0 讨论(0)
  • 2020-12-28 13:33

    I found the best anwser, it work for me. Clear the value of bootstrap-datepicker:

    $('#datepicker').datepicker('setDate', null);
    

    Add value for boostrap-datepicker:

    $('#datepicker').datepicker('setDate', datePicker);
    
    0 讨论(0)
  • 2020-12-28 13:40

    Actually it is much more useful use the method that came with the library like this $(".datepicker").datepicker("clearDates");

    I recommend you to always take a look at the documentation of the library, here is the one I used for this.

    bootstrap-datepicker methods documentation

    0 讨论(0)
  • 2020-12-28 13:41

    I came across this thread while trying to clear the date already set. Attempting:

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

    produced:

    TypeError: t.dpDiv is undefined 
    https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js -- Line 8
    

    Thinking that perhaps one needed to include the original Datepicker for bootstrap removes the error, but then causes the original Datepicker widget to appear! - so that's obviously wrong and not needed.

    Looking further, the crash in jqueryui is in:

        /* Generate the date picker content. */
    _updateDatepicker: function(inst) {
        this.maxRows = 4; //Reset the max number of rows being displayed (see #7043)
        datepicker_instActive = inst; // for delegate hover events
        inst.dpDiv.empty().append(this._generateHTML(inst));
        this._attachHandlers(inst);
    

    and inst.pdDiv does not exist.

    Investigating further - I realized that what was needed was:

            $formControl = $duedate.find("input");
            $formControl.val('');
            $formControl.removeData();
    

    Which solved my problem. Note I also needed the $formControl.removeData() otherwise the state of the widget would not be reinitialized to the initial state.

    I hope this helps someone else. :-)

    0 讨论(0)
提交回复
热议问题