JQuery Datepicker日期仅在单击日期时捕获

JQuery Datepicker date is captured only when the day is clicked

本文关键字:日期 单击 Datepicker JQuery      更新时间:2023-09-26

jQuery Datepicker遇到了一个奇怪的问题。我已经设置了选项changeMonth: true以启用月份更改。

例如,如果我的日期选择器显示日期02/05/2013,当我只是将月份更改为4月(不单击控件中的任何一天)时,日期显示为02/04/2013。但是当我从控件获取日期时,我最终得到的是旧的月份值,而不是新选择的月份值。代码:

$('#uxStartDateTime').datetimepicker({
    timeFormat: "hh:mm tt",
    dateFormat: "dd/mm/yy",
    showButtonPanel: false,
    changeMonth: true,
    changeYear: true
});
//getting the date
var startDate = $('#uxStartDateTime').datetimepicker('getDate');
//formatting this date to it send via ajax to c#
startDate = startDate.getFullYear() + "-" + (startDate.getMonth() + 1) + "-" + startDate.getDate() + " " + startDate.getHours() + ":" + startDate.getMinutes();

是我的方法有什么问题,还是这是一个已知的问题?

正如adeneo在他的评论中指出的那样,这是预期的日期拾取器行为。如果你想改变这一点,你将不得不编写一个方法,在每月变化时触发。

$('.ui-datepicker-month').change(function () {
    // if there is a value in your controller, change it
});

我将把日期加上/减去月份的实际逻辑留给你。