JQuery Datepicker, can't trigger onSelect event manually!

前端 未结 9 990
爱一瞬间的悲伤
爱一瞬间的悲伤 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:07
    $('.ui-datepicker-current-day').click();
    
    0 讨论(0)
  • 2021-01-05 00:09

    I was trying to do almost the same thing, but in my case I have to shoe appointments on load and not on selection.

    Although this is not necessary for your solution, I'd like to add my solution because it might help others who want to do the same on load.

    I modified the jquery.ui.datepicker.mobile.js to highlight days with appointments with beforeShowDay, load appointment onSelect etc. You can simply initialize your appointments by adding the following lines at the bottom of the datepicker rewriting function:

    //rewrite datepicker
    $.fn.datepicker = function( options ){
    
        // ... all the options and event stuff
    
        function initAppointments() {
            // select the 'selected' days to trigger the onSelect event#
            // and initalize the appointments within the event handler
            $( ".ui-datepicker-calendar a.ui-state-active", dp ).trigger('click');
            updateDatepicker();
        }
    
        //update now
        updateDatepicker();
    
        // and on click
        $( dp ).click( updateDatepicker );
        $( dp ).ready( initAppointments ); // new
    
        //return jqm obj 
        return this;
    };
    
    0 讨论(0)
  • 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(); 
    
    });
    
    0 讨论(0)
  • 2021-01-05 00:25

    there is another solution for find out when a date select.

     $(".uc_datepicker :text").datepicker();
     $(".uc_datepicker :text").click(function() {
        $(".ui-datepicker-calendar a").click(function() {
            alert("date selected");
        });
     });
    
    0 讨论(0)
  • 2021-01-05 00:27

    This is how I got round the same problem. Simply register an event with the code you want to call, then call that event from the onSelect method inside datepicker and then call that same event any other time you want.

    $(document).ready(function(){
    
        // Onselect event registration
        $("#date_calendar").bind("datepickeronSelect", function(){
    
            alert("onSelect called!");
    
        })
    
        //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)
            {
                $("#date_calendar").trigger("datepickeronSelect");
            }
        }).disableSelection().trigger("datepickeronSelect");
    });
    
    0 讨论(0)
  • 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"
            );
    }
    
    0 讨论(0)
提交回复
热议问题