JQuery Datepicker, can't trigger onSelect event manually!

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

    You could try something like this -

        $(document).ready(function(){
        //when page loads                      
        SetDateTime(null);
        //create date pickers
        $("#date_calendar").datepicker(
      { 
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy-mm-dd',
            defaultDate: $.datepicker.parseDate("y-m-d", $('#date').val()),
            onSelect: function(dateText, inst)
                {
                    SetDateTime(dateText);
                }
        }).disableSelection();
    
        $("#date_calendar").trigger('onSelect');
    });
    
    function SetDateTime(selectedDate)
    {
        if(selectedDate == null)
        {
            //get todays date
            var dateNow = new Date();
            //if its a single digit day, add a zero before it
            var sDay = dateNow.getDate().toString();
            if(sDay.length == 1)
            {
                sDay = "0" + sDay;
            }
            selectedDate = dateNow.getFullYear() + "-" + (dateNow.getMonth() + 1) + "-" + sDay;
    
            //load timetable
            ShowDay(selectedDate);
        }
        else
        {
            var ds = selectedDate.split("-");
    
            //load timetable
            ShowDay(selectedDate);  
        }
    }
    
    function ShowDay(dateToday)
    {
             alert('onSelect triggered! Yay!');
             $('#date').val($.datepicker.formatDate("yy-mm-dd", $('#date_calendar').datepicker('getDate')));
    
            // Ajax for populating days when selected
            $.post(
                "server_requests/show_day.php",
                    {
                    date: dateToday,
                    user_id: $('#user_id').val()
                    },
                function(data)
                {
                    //return function
                    $('#my_day_tasks').html(data.resultTable);
                },
                "json"
            );
    }
    

提交回复
热议问题