jQuery Datepicker: Prevent closing picker when clicking a date

后端 未结 9 2166
南方客
南方客 2020-11-29 08:16

Hi fellow stackoverflow:ers,

I\'m using the jQuery Datepicker plugin, together with Martin Milesich Timepicker plugin. Everything works great, except for the fact th

相关标签:
9条回答
  • 2020-11-29 08:56

    well this a dirty workaround...but it works for me...even with showButtonPanel: true

      $(function() {
        var dp_c = null, dp_once = false;
        $( '#datepicker' ).datepicker({
          showButtonPanel: true,
          onSelect: function() {
            $(this).data('datepicker').inline = true;  
            setTimeout(function () {
              $('#ui-datepicker-div').find('.ui-datepicker-buttonpane').append(dp_c);
            }, 1);
    
          },
          onClose: function() {
            $(this).data('datepicker').inline = false;
          }
        }).click(function () {
          if(!dp_once) {
            setTimeout(function () {
              dp_c = $('#ui-datepicker-div').find('.ui-datepicker-close').clone();
            }, 500);
            dp_once = !!1;
          }
        });
    
        $('#ui-datepicker-div').on('click', '.ui-datepicker-close', function () {
          $('#datepicker').datepicker( "hide" );
        });
    
      });
    
    0 讨论(0)
  • 2020-11-29 08:57

    Cool, if you are using jquery-ui-1.8.5.custom.min.js and jquery-ui.multidatespicker.js, you could modify the jquery-ui-1.8.5.custom.min.js: from:

    if(a.inline)this._updateDatepicker(a);
    

    into:

    if(a.inline || !this._get(a, 'closeOnSelect'))this._updateDatepicker(a);
    
    0 讨论(0)
  • 2020-11-29 08:59

    Here is a solution :

    onSelect: function ( dateText, inst ) {
        ..... // Your code like $(this).val( dateText );`
    
    
        //Set inline to true to force "no close"
        inst.inline = true;
    },
    onClose: function(date,inst){
        //Set inline to false => datapicker is closed
        // onClose is called only if you click outside the datapicker, onSelect will not
        // trig onClose due to inline = true
       inst.inline = false;
    }
    

    `

    0 讨论(0)
  • 2020-11-29 09:00

    For reference, and since people have asked me this through mail. Here's a code chunk that needs to be added to timepicker.js:

    /**
     * Don't hide the date picker when clicking a date
     */
    $.datepicker._selectDateOverload = $.datepicker._selectDate;
    $.datepicker._selectDate = function(id, dateStr) {
        var target = $(id);
        var inst = this._getInst(target[0]);
        inst.inline = true;
        $.datepicker._selectDateOverload(id, dateStr);
        inst.inline = false;
        this._updateDatepicker(inst);
    }
    

    Good luck in getting it working on your site!

    0 讨论(0)
  • 2020-11-29 09:01

    You should override native js function:

     /* Update the input field with the selected date. */
            _selectDate: function(id, dateStr) {
                var target = $(id);
                var inst = this._getInst(target[0]);
                dateStr = (dateStr != null ? dateStr : this._formatDate(inst));
                if (inst.input)
                    inst.input.val(dateStr);
                this._updateAlternate(inst);
                var onSelect = this._get(inst, 'onSelect');
                if (onSelect)
                    onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);  // trigger custom callback
                else if (inst.input)
                    inst.input.trigger('change'); // fire the change event
                if (inst.inline)
                    this._updateDatepicker(inst);
                else {
                    if(inst.settings.hideOnSelect != false){
                        this._hideDatepicker();
                    }
                    this._lastInput = inst.input[0];
                    if (typeof(inst.input[0]) != 'object')
                        inst.input.focus(); // restore focus
                    this._lastInput = null;
                }
            },
    

    And add appropriate option to datepicker configuration, as example:

    var defaultDatePickerOptions = {
            hideOnSelect: false,
            ...
        }; 
     var birthDate = jQuery.extend({}, defaultDatePickerOptions);
     $('#birthdate').datepicker(birthDate);
    
    0 讨论(0)
  • 2020-11-29 09:08

    rather than changing the source it's best to use the existing events

    onSelect: function() {
        $(this).data('datepicker').inline = true;                               
    },
    onClose: function() {
        $(this).data('datepicker').inline = false;
    }
    
    0 讨论(0)
提交回复
热议问题