简体   繁体   中英

How to set jQuery datapicker current date to input tag value?

I would like to set a value to input tag on which I create a datepicker. When datepicker is initialized I want to set it. Here is an some pseudo example what I want:

HTML:

<input id="test" value=""></input>

Javascript:

$('#text').datepicker({ dateFormat: 'dd.mm.yy'});
var currentDate = $('#text').datepicker('getDate');
$('#text').val(currentDate.format('dd.mm.yy'));

Thanks in advance!

If you are trying to set a default date for the datePicker plugin, you might want to use the defaultDate option.

Extract from JQuery UI/datepicker docs :

defaultDate: Date, Number, StringDefault:null
Set the date to highlight on first opening if the field is blank. Specify either an actual date via a Date object or as a string in the current dateFormat, or a number of days from today (eg +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, eg '+1m +7d'), or null for today.

Code examples
Initialize a datepicker with the defaultDate option specified.

$( ".selector" ).datepicker({ defaultDate: +7 });

Get or set the defaultDate option, after init.

//getter
var defaultDate = $( ".selector" ).datepicker( "option", "defaultDate" );
//setter
$(".selector").datepicker( "option", "defaultDate", +7 );

All this you can easily find in the JQuery docs found here: http://docs.jquery.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM