JQuery DatePicker ReadOnly

后端 未结 14 2283
温柔的废话
温柔的废话 2020-12-03 04:35

When I make my datepicker read-only, I see that the user cannot type anything into the text box.

$(\"#datepicker\").attr(\'readonly\', \'readonly\');


        
相关标签:
14条回答
  • 2020-12-03 05:01

    I solved this with selector. I do not initialize the datetime picker on inputs which are readonly.

    $('[type=datetime]:not([readonly])').datetimepicker();
    
    0 讨论(0)
  • 2020-12-03 05:05

    onkeypress = > preventdefault ...

    0 讨论(0)
  • 2020-12-03 05:06

    If you're trying to disable the field (without actually disabling it), try setting the onfocus event to this.blur();. This way, whenever the field gets focus, it automatically loses it.

    0 讨论(0)
  • 2020-12-03 05:08

    During initialization you have to set to false the option enableOnReadonly which by default is true.

    $('#datepicker').datepicker({
        enableOnReadonly: false
    });
    

    Then, you need to make the field readonly

    $('#datepicker').prop('readonly', true);
    

    See https://bootstrap-datepicker.readthedocs.io/en/latest/options.html#enableonreadonly

    0 讨论(0)
  • 2020-12-03 05:10

    My final solution:

    When my app loads I initialize the datepicker as such:

    $('.selector').datepicker({ dateFormat: "mm/dd/yy" }); 
    

    I then have a global toggle button that enables/disables the datepicker. To disable I do the following:

    $('.selector').datepicker("option", "minDate", -1);
    $('.selector').datepicker("option", "maxDate", -2); 
    

    To enable I do the following:

    $('.selector').datepicker("option", "minDate", null);
    $('.selector').datepicker("option", "maxDate", null); 
    

    Thanks everyone!

    0 讨论(0)
  • 2020-12-03 05:10
          beforeShow: function(el) {
                if ( el.getAttribute("readonly") !== null ) {
                    if ( (el.value == null) || (el.value == '') ) {
                        $(el).datepicker( "option", "minDate", +1 );
                        $(el).datepicker( "option", "maxDate", -1 );
                    } else {
                        $(el).datepicker( "option", "minDate", el.value );
                        $(el).datepicker( "option", "maxDate", el.value );
                    }
                }
            },
    
    0 讨论(0)
提交回复
热议问题