使用下一个箭头时,在jqueryui的日期选择器中返回了错误的月份值

wrong value returned for month in datepicker of jquery ui when using the next arrow

本文关键字:返回 选择器 错误 日期 下一个 jqueryui      更新时间:2023-09-26

我使用datpicker来提供仅选择月份/年份的选项,当选择月份时,将提交具有所选值的表单。

这是代码:jquery ui日期选择器使用此代码:

    $(document).ready(function(){
        $("#datepicker").datepicker( {
            changeMonth: true,
            changeYear: true,
            showButtonPanel: false,
            yearRange: "2013:2015",
            onChangeMonthYear: function(dateText, inst) { 
                console.log($(".ui-datepicker-year :selected",$(this)).val() 
                    + "/" + $(".ui-datepicker-month :selected",$(this)).val());
            }
        });
    });

但是有两种情况下返回的数字是错误的。

(console.log中的值)

  1. 如果您尝试选择一个月,比如说2013年10月,返回的值是2013/9,这是可以的。现在,如果你试图点击箭头转到2013年11月,那么值再次为2013/9,这是错误的。从2013年12月到2012年1月的类似跳跃返回2013/11

  2. 如果你转到"2015年12月"的末尾,然后单击下一个箭头,它会回到开始的"2013年1月",这是可以的,但继续单击下一箭头,你会注意到你不能再从"2013年12月"移动到"2014年1月",它会移回"2013年3月"

任何帮助都将不胜感激。

onChangeMonthYear()接收新选择的年份和月份作为参数,因此您可以直接使用这些

            ...
            minDate: new Date(2013, 1 - 1, 1),
            maxDate: new Date(2015, 12 - 1, 31),
            onChangeMonthYear: function(year, month, inst) { 
                console.log(
                    year + "/" + month
                );
            }