jQuery问题:点击上下月按钮后修改“今日月份”为对应月份名
I’ve tackled this exact need before, and here’s a solid solution to update the "Todays month" label to match the navigated month when clicking the previous/next month buttons in the Keith Wood Datepick plugin:
JavaScript Implementation
$('#defaultPopup').datepick({ onChangeMonthYear: function(year, month, inst) { // Create a date object for the first day of the selected month/year const selectedDate = new Date(year, month - 1, 1); // Format to get the full month name const changedMonth = $.datepick.formatDate('MM', selectedDate); // Update the "Today" button's text to the selected month $('.datepick a.datepick-cmd-today').html(changedMonth); console.log(changedMonth); // Verify the output in your browser console }, });
How It Works
- The
onChangeMonthYearcallback triggers automatically whenever the user switches to a new month or year via the navigation buttons. - We build a valid Date object using the provided
yearandmonthparameters (note: JavaScript uses 0-indexed months, so we subtract 1 from the plugin’s 1-indexed month value to avoid off-by-one errors). - Using
$.datepick.formatDate('MM', ...)generates the full, human-readable name of the selected month (like "January" or "October"). - Finally, we target the "Today" command button and replace its inner text with the formatted month name to match the currently displayed calendar view.
This method reliably keeps the "Todays month" label in sync with the calendar’s visible month whenever you navigate with the previous/next controls.
内容的提问来源于stack exchange,提问作者RhymeGuy
相关产品推荐
相关产品推荐

