问题
Given this markup:
// Calendar.html?date=1/2/2003
<script>
$(function() {
$('.inlinedatepicker').datepicker();
});
</script>
...
<div class="inlinedatepicker" id="calendar"/>
This will display an inline datepicker with very little effort (awesome!). How do I preset the datepicker to the date passed in via the query string?
Note that in this case, I don't have an input box--just a calendar attached to a div.
回答1:
This should work, though you may run into locale issues (specifically, M/D/Y vs. D/M/Y).
var date = location.match(/(?:\?|&)date=(\d+\/\d+\/\d+)(&|$)/)[1];
$('.inlinedatepicker').datepicker().datepicker("setDate", new Date(date));
回答2:
I managed to do this with
$('.inlinedatepicker').datepicker().datepicker("setDate", new Date(2011,11,01)); // Date equals December 1, 2011.
I had to subtract one from the month though to get the right date as it seems that date is starting from zero (0 = January)
回答3:
in addition to @Ben Blank answer you can use defaultDate like this
var date = location.match(/(?:\?|&)date=(\d+\/\d+\/\d+)(&|$)/)[1];
$('.inlinedatepicker').datepicker({defaultDate:new Date(date)});
来源:https://stackoverflow.com/questions/879703/how-do-i-initialize-a-jquery-ui-datepicker-to-a-date-from-the-querystring