如何基于现有jQuery UI Timepicker配置实现选当前时间并隐藏当日已过时点?
Hey there! Let's walk through how to make your jQuery UI Timepicker meet your two key requirements: auto-selecting the current time, and hiding any times that have already passed today. Here's the step-by-step solution:
To ensure only current and future times are visible, we need to set the minTime option to the current time. Since you've enabled forceRoundTime: true with a 15-minute step, the timepicker will automatically round up the current time to the nearest valid interval (e.g., if it's 10:08 AM, it'll set the minimum time to 10:15 AM).
We'll add the defaultTime: "now" option to pre-fill the input with the rounded current time, and keep your existing scrollDefault: "now" to ensure the dropdown menu scrolls directly to this time when opened.
Modified Full Initialization Code
// Grab the current date/time to use as our minimum threshold const currentTime = new Date(); $("#ReminderTime").timepicker({ "showDuration": true, "timeFormat": "g:i A", "step": 15, "forceRoundTime": true, "maxTime": "11:45pm", "className": "timepicker-width", "scrollDefault": "now", // Hide any times before the current moment "minTime": currentTime, // Pre-fill the input with the rounded current time "defaultTime": "now" });
Key Notes on the Changes:
minTime: currentTime: This tells the timepicker to filter out all times that occur before the current moment. TheforceRoundTimesetting will adjust this to the next valid 15-minute interval if the current time isn't already aligned with your step.defaultTime: "now": This ensures the input field starts with the rounded current time selected, so users don't have to manually choose it. Combined withscrollDefault: "now", the dropdown will automatically focus on the current time when opened.- Your existing
maxTime: "11:45pm"will still work as expected—any times beyond this will be hidden, even if they fall after the current time.
内容的提问来源于stack exchange,提问作者Priyanka Khade

