代码之家  ›  专栏  ›  技术社区  ›  Emil Stenström

jQuery日期选择器:在单击日期时防止关闭选择器

  •  16
  • Emil Stenström  · 技术社区  · 17 年前

    大家好,斯塔克尔斯先生,

    我在用电话 jQuery Datepicker plugin ,连同 Martin Milesich Timepicker 插件。一切都很好,除了在datepicker中单击日期会关闭小部件,没有时间选择时间。

    online at the timepicker demo

    任何解决此问题的帮助,甚至是正确方向的指点,都将不胜感激!

    更多信息: 我正在使用Martins下载链接提供的文件: http://milesich.com/tpdemo/timepicker-0.2.0.zip

    • jquery-ui-1.7.2.custom.min.js
    • timepicker.js(最新版本0.2.0)

    $(document).ready(function(){
        $(".datepicker").datepicker({
            duration: '',  
            showTime: true,  
            constrainInput: false,  
            stepMinutes: 5,  
            stepHours: 1, 
            time24h: true,
            dateFormat: "yy-mm-dd",
            buttonImage: '/static/images/datepicker.png',
            buttonImageOnly: true,
            firstDay: 1,
            monthNames: ['Januari','Februari','Mars','April','Maj','Juni','Juli','Augusti','September','Oktober','November','December'],
            showOn: 'both',
            showButtonPanel: true
         });
    })
    
    9 回复  |  直到 17 年前
        1
  •  28
  •   Emil Stenström    16 年前

    /**
     * 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);
    }
    

    祝你好运,让它在你的网站上工作!

        2
  •  28
  •   Min Hur    14 年前

    与其更改源,不如使用现有事件

    onSelect: function() {
        $(this).data('datepicker').inline = true;                               
    },
    onClose: function() {
        $(this).data('datepicker').inline = false;
    }
    
        3
  •  8
  •   Sergey    12 年前

    你必须亲自破解日期选择器。这是它使用的代码。如果它不是内联的,则当您选择日期时,它将隐藏。

    您可以传入自己的onSelect方法,快速将datePicker实例更改为内联,然后将其更改回内联,而无需更改datePicker的内部结构,但这是一个非常粗糙的解决方案。

    if (inst.inline)
            this._updateDatepicker(inst);
    else {
          this._hideDatepicker(null, this._get(inst, 'duration'));
          this._lastInput = inst.input[0];
          if (typeof(inst.input[0]) != 'object')
          inst.input[0].focus(); // restore focus
          this._lastInput = null;
    }
    
        4
  •  3
  •   TomPAP    12 年前

    以下是一个解决方案:

    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;
    }
    

    `

        5
  •  3
  •   Mina Luke    10 年前

    为什么所有评论都提供了解决方法?它是straigtfarword:

    将内联日历与altField一起使用:)

        <input type="text" id="alternate" size="30" />
        <div id="datepicker"></div>
    
        <script>
           $("#datepicker").datepicker({
              altField: "#alternate"
           });
        </script>
    

    http://jsfiddle.net/menocomp/y2s662b0/1/

        6
  •  0
  •   diyism    15 年前

    发件人:

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

    进入:

    if(a.inline || !this._get(a, 'closeOnSelect'))this._updateDatepicker(a);
    
        7
  •  0
  •   s4ty    13 年前

    好吧,这是一个肮脏的解决办法…但它对我有效…即使有 showButtonPanel:对

      $(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" );
        });
    
      });
    
        8
  •  0
  •   Ihor Kruk    12 年前

    您应该重写本机js函数:

     /* 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;
                }
            },
    

    并向datepicker配置添加适当的选项,例如:

    var defaultDatePickerOptions = {
            hideOnSelect: false,
            ...
        }; 
     var birthDate = jQuery.extend({}, defaultDatePickerOptions);
     $('#birthdate').datepicker(birthDate);
    
        9
  •  0
  •   Lukasz 'Severiaan' Grela    11 年前

    首先,我在小部件上的defaults dict中添加了另一个属性:

    closeOnSelect:true //True to close the widget when you select a date
    

    其次,在_selectDate方法中找到此语句:

    if (inst.inline)
            this._updateDatepicker(inst);
    else {
            ...
        }
    

    var closeOnSelect = this._get(inst, "closeOnSelect");        
    if (inst.inline || !closeOnSelect)
            this._updateDatepicker(inst);
    else {
            ...
        }
    

    试试看,它对我有用。我是在jQueryUI1.8.5版本上完成的。

    推荐文章