如何配置jQuery Datepicker以选中当前日期?
Hey there! To get your jQuery Datepicker to default to the current date instead of tomorrow, you just need to tweak one line in your configuration. Here's the updated code and breakdown:
Updated Code
HTML (no changes needed here)
<input type="text" id="datepicker">
JavaScript (modified line highlighted)
<script> $( function() { $( "#datepicker" ).datepicker({ dateFormat: 'mm-dd-yy', minDate: "today", maxDate: "+30d", setDate: "today" // Replaced "+1d" with "today" }); }); </script>
Alternative Explicit Option
If you prefer using a native JavaScript date object instead of the built-in shortcut, you can also set setDate to new Date():
setDate: new Date()
What Changed & Why It Works
- The original code used
"+1d"forsetDate, which adds one day to the current date (hence selecting tomorrow). - Switching to
"today"uses jQuery UI Datepicker's built-in shortcut to select the current date by default. - Your existing
minDate: "today"setting ensures users can't pick dates before today, which aligns perfectly with the default current date selection.
内容的提问来源于stack exchange,提问作者omkar
相关产品推荐
相关产品推荐

