JQuery Datepicker, can't trigger onSelect event manually!

前端 未结 9 991
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 00:00

I am using jquery\'s datepicker where a list of items is populated from an ajax call whenever a date is picked from an inline datepicker object. The script works perfect exc

相关标签:
9条回答
  • 2021-01-05 00:28

    Here is what I came up with and seems to be the best solution:

    var inst = $.datepicker._getInst($("#date")[0]);
    $.datepicker._get(inst, 'onSelect').apply(inst.input[0], [$("#date").datepicker('getDate'), inst]);
    

    And it works like a charm

    0 讨论(0)
  • 2021-01-05 00:29

    The accepted answer was not something I could use, because I was using common code that added hotkey functionality on all datepickers in my web application (to set value to today, +1 day, +1 month, etc.), and wanted to make sure onselect was called to validate the date value that was calculated with the hot key.

    I took this from the source code of the datepicker.js file. Note thiat this should be replaced with your datepicker element. In my case, I was calling this from a keydown event.

    // this is SUCH a hack.  We need to call onselect to call any
    // specific validation on this input.  I stole this from the datepicker source code.
    var inst = $.datepicker._getInst(this),
    onSelect = $.datepicker._get(inst, "onSelect");
    if (onSelect) {
        dateStr = $.datepicker._formatDate(inst);
        onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);
    }
    
    0 讨论(0)
  • 2021-01-05 00:32

    I use another way, trigger 2 events

    1. setDate jQuery('#my-calendar').datepicker( "setDate", "09/29/2020" )
    2. Trigger click event jQuery('#my-calendar .ui-state-active').trigger('click')
    0 讨论(0)
提交回复
热议问题