JQuery Datepicker, can't trigger onSelect event manually!

前端 未结 9 1044
爱一瞬间的悲伤
爱一瞬间的悲伤 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:22

    Couldn't you just refactor that to a function of its own, which you reuse? Strictly speaking, a datepicker select is not really what happens on page load. You just want to do exactly the same thing that happens when the datepicker is indeed selected.

    function populateList(dateText, inst)
    {
        alert('alert test');
        $('#date').val($.datepicker.formatDate("yy-mm-dd",$('#date_calendar').datepicker('getDate')));
    
        // Ajax for populating days when selected
        $.post("server_requests/show_day.php",
            {
                date: $('#date').val(),
                user_id: $('#user_id').val()
            },
            function(data)
            {
                //return function
                $('#my_day_tasks').html(data.resultTable);
            },
            "json"
        );
    }
    
    $(document).ready(function(){
      //create date pickers
      $("#date_calendar").datepicker(
      { 
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy-mm-dd',
            defaultDate: $.datepicker.parseDate("y-m-d", $('#date').val()),
            onSelect: populateList
      }).disableSelection();
    
      // i'm not bothering to pass the input params here, because you're not using them anyway
      populateList(); 
    
    });
    

提交回复
热议问题