javascript - Datepicker set the selected date -
this code, page called cal.html:
<html lang="en"> <head> <meta charset="utf-8"> <title>jquery ui datepicker - default functionality</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="http://code.jquery.com/jquery-1.10.2.js"></script> <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <link rel="stylesheet" href="http://code.jquery.com/resources/demos/style.css"> <script> $(function() { $.datepicker.setdefaults($.datepicker.regional['it']); $('#datepicker').datepicker({ inline: true, altfield: '#datascelta', onselect: function(){ $('#formscelta').submit(); } }); }); </script> <div id="datepicker"></div> <form id="formscelta" action="cal.html" method="post"> <input type="hidden" name="datascelta" id="datascelta"> </form> </head>
want wanna when select date in datepicker , page reloaded set date in datepicker selected date. possible?
to store date , after reloading page have use cookies, can use cookies (js-cookie
or jquery cookie
) or use localstorage
, see code exmples bellow :
using js-cookie :
cookies.set('datascelta', datavalue); //set datascelta cookies.get('datascelta'); //get datascelta
full code :
$(function () { $.datepicker.setdefaults($.datepicker.regional['it']); $('#datepicker').datepicker({ inline: true, altfield: '#datascelta', defaultdate: cookies.get('datascelta') ? cookies.get('datascelta') : new date() //get date after reload , init datepicker onselect: function (datavalue) { cookies.set('datascelta', datavalue); //store date in cookies before submiting form $('#formscelta').submit(); } }); });
or can use jquery cookie :
$.cookie('datascelta', datavalue); //set datascelta $.cookie('datascelta'); //get datascelta
full code :
$(function () { $.datepicker.setdefaults($.datepicker.regional['it']); $('#datepicker').datepicker({ inline: true, altfield: '#datascelta', defaultdate: $.cookie('datascelta') ? $.cookie('datascelta') : new date() //get date after reload , init datepicker onselect: function (datavalue) { $.cookie('datascelta', datavalue); //store date in cookies before submiting form $('#formscelta').submit(); } }); });
you can use localstorage
take @ local storage vs cookies.
using local storage code following :
localstorage['datascelta'] = datavalue; //set datascelta localstorage['datascelta']; //get datascelta
full code :
$(function () { $.datepicker.setdefaults($.datepicker.regional['it']); $('#datepicker').datepicker({ inline: true, altfield: '#datascelta', defaultdate: localstorage['datascelta'] ? localstorage['datascelta'] : new date() //get date after reload , init datepicker onselect: function (datavalue) { localstorage['datascelta'] = datavalue; //store date in cookies before submiting form $('#formscelta').submit(); } }); });
hope help.
Comments
Post a Comment