问题
I'm using a jQuery datepicker attached to a text input. Is there a datepicker action that can inialize the text field w/ today's date (yyyy-mm-dd) when the page loads?
回答1:
Initialize the datepicker with the date format and set the date -
$( ".selector" ).datepicker({ dateFormat: 'yy-mm-dd'})
$( ".selector" ).datepicker( "setDate" , new Date())
回答2:
You can set a default date to a fixed or relative value:
// Only necessary if you are sending data using a different format
$( ".selector" ).datepicker({ altFormat: 'yy-mm-dd' });
// Initialize to a fixed date
$( ".selector" ).datepicker({ defaultDate: '2011-10-20' });
// or to a relative date
$( ".selector" ).datepicker({ defaultDate: +7 });
http://jqueryui.com/demos/datepicker/#option-altFormat
http://jqueryui.com/demos/datepicker/#option-defaultDate
回答3:
Assuming that you're using the datepicker from jQueryUI, you can explicitly set its value directly after initializing it:
$('#id').datepicker();
$('#id').datepicker('setDate', new Date);
来源:https://stackoverflow.com/questions/7837107/inialize-a-jquery-textfield-datepicker-w-todays-date-in-the-format-yyyy-mm-dd