javascript jQuery datepicker 显示的月份和年份不是当前的
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15044668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery datepicker get month and year shown not current
提问by Matt
I'm trying to get the "shown" month and year after hitting the "next" and "previous" links on the left and right of the date picker header.
我试图在点击日期选择器标题左侧和右侧的“下一个”和“上一个”链接后获得“显示”的月份和年份。
Currently I'm using to get the month:
目前我正在使用获取月份:
var selectedMonth = $('#calendar').datepicker('getDate').getMonth() + 1;
but it only returns the current month, not the shown month that comes with clicking "next" or "prev".
但它只返回当前月份,而不是单击“下一个”或“上一个”时显示的月份。
EDIT
编辑
I added the onChangeMonthYear
option with the datepicker in the function i have
我onChangeMonthYear
在我拥有的功能中添加了带有日期选择器的选项
function getDates() {
var selectedMonth = $('.ui-datepicker-month').text();
var selectedYear = $('.ui-datepicker-year').text();
}
still this just states the current date.
这仍然只是说明当前日期。
EDITED Again
再次编辑
the above worked just needed to set a delay :]
上述工作只需要设置延迟:]
回答by MushinNoShin
$.datepicker('getDate')
only returns the selected date of the datepicker, not the month that is currently being shown.
$.datepicker('getDate')
只返回日期选择器的选定日期,而不是当前显示的月份。
What you're looking for is to bind a function to onChangeMonthYear
. Docs here
您正在寻找的是将函数绑定到onChangeMonthYear
. 文档在这里
Example:
例子:
$('#calendar').datepicker('option', 'onChangeMonthYear', function(year, month) { var selectedMonth = month })