Inialize a jQuery textfield-datepicker w/ today's date in the format yyyy-mm-dd

隐身守侯 提交于 2019-12-23 19:58:51

问题


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

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