最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Jquery datepicker execute function on load - Stack Overflow

matteradmin7PV0评论

I have the following script that works fine:

  $(function() {
    $( "#datepicker" ).datepicker
(
{
      altField: '#sheetid',
      onSelect: function load() {
        $(document).ready(function() {
                $.ajax({
                type: 'POST',
                url: 'test2.php',
                data: {sheetid: $('#sheetid').val()},
                success: function(data)
                {
                    $("#contentz").html(data);
                }
            });

    });
    },
      firstDay: 1});
}
);

The script loads data in a div based on the selected date from an inline jq datepicker. But I can't seem to succeed on making it work for the default date, when the page first loads. I've tried:

$("#datepicker").load(function() {
$(document).ready(function() {
                    $.ajax({
                    type: 'POST',
                    url: 'test2.php',
                    data: {sheetid: $('#sheetid').val()},
                    success: function(data)
                    {
                        $("#contentz").html(data);
                    }
                });

        }
})

but that doesn't seem to work either. Any suggestions?

I have the following script that works fine:

  $(function() {
    $( "#datepicker" ).datepicker
(
{
      altField: '#sheetid',
      onSelect: function load() {
        $(document).ready(function() {
                $.ajax({
                type: 'POST',
                url: 'test2.php',
                data: {sheetid: $('#sheetid').val()},
                success: function(data)
                {
                    $("#contentz").html(data);
                }
            });

    });
    },
      firstDay: 1});
}
);

The script loads data in a div based on the selected date from an inline jq datepicker. But I can't seem to succeed on making it work for the default date, when the page first loads. I've tried:

$("#datepicker").load(function() {
$(document).ready(function() {
                    $.ajax({
                    type: 'POST',
                    url: 'test2.php',
                    data: {sheetid: $('#sheetid').val()},
                    success: function(data)
                    {
                        $("#contentz").html(data);
                    }
                });

        }
})

but that doesn't seem to work either. Any suggestions?

Share Improve this question edited Sep 9, 2013 at 11:43 tlenss 2,6092 gold badges23 silver badges26 bronze badges asked Sep 9, 2013 at 11:26 user2743057user2743057 1693 gold badges5 silver badges11 bronze badges 1
  • 1 of course it doesn't work, you are binding to document.ready event from datepicker.load event... try other way around – Ivan Hušnjak Commented Sep 9, 2013 at 11:41
Add a ment  | 

3 Answers 3

Reset to default 1

enter image description here

If you want to set date onload use this function.

$("#datepicker").datepicker();
$("#datepicker").datepicker("setDate", new Date());

I would do it like this:

$(document).ready(function() {
    $("#datepicker").load(function() {
        $.ajax({
            type: 'POST',
            url: 'test2.php',
            data: {sheetid: $('#sheetid').val()},
            success: function(data)
            {
                $("#contentz").html(data);
            }
        });
    });
});

it makes no sense to bind something to the document.ready event on datepicker.load. You can do what you want directly there.

As far as I see, there is nothing asynchronous here. So just do it consecutively:

$(function() {
    $("#datepicker").datepicker({});
    // your function with ajax call here
    console.log( $("#ui-datepicker-div").length );
});

This effectively logs "1".

Post a comment

comment list (0)

  1. No comments so far