How do I initialize a jQuery UI datepicker to a date from the querystring?

元气小坏坏 提交于 2021-01-27 04:51:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!