首页 > 解决方案 > jQuery Datepicker onClose 上的 SetDate 不会改变值

问题描述

我有一张桌子,每一行都有一个日期选择器。Datepickers 只选择月份,并且在关闭时我设置了该月的第一天,除非这一天在 minDate 之前,所以我想在那里设置 minDate。

SetDate 函数不起作用。我的日志告诉我正确设置了正确的日期,但是当我在 setDate 中传递它时,结果不会改变,并且它以某种方式堆叠到本月的第一天。我错过了什么?

    function initDatepickerSchedule(){  

        $(".datepickerSchedule").each(function(){

            $(this).datepicker({
                showOn: "both",
                changeMonth: true,
                changeYear: true,
                dateFormat: "yy-mm-dd",
                minDate: scheduleTaskMinDate(),
                maxDate: scheduleTaskMaxDate($(this).closest("tr").find(".ScheduledTaskQtyInput").val(),$(this).closest("tr").find(".ScheduledTaskCompletedQtyInput").val()),
                showButtonPanel: true,  
                onClose: function(dateText, inst) { 
                    function isDonePressed(){
                       return $('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1;
                    }

                    if (isDonePressed()){
                        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                        var properDate=new Date(year, month, 1);
                        var minDate=$(this).datepicker("option","minDate");                  
                        console.log(properDate);// 2019-12-1
                        console.log(minDate);//2019-12-2
                        console.log(properDate<minDate);// true
                        if(properDate<minDate){
                            properDate=minDate;
                        }
                        console.log("before set");
                        console.log(properDate===minDate);//true
                        console.log(properDate);//2019-12-2

                        $(this).datepicker("setDate",properDate);
                        console.log($(this).datepicker("getDate"));//2019-12-1
                        var coNo=$(this).closest("tr").find(".changeOrderNoInput").val();
                        $(this).closest("tr").find(".ScheduledTaskVersion").val(studyTaskScheduleVersion($(this).datepicker("getDate"),coNo));
                        $("#SubmitScheduleDistButton").prop('disabled', true).prop('title',"Validate without Errors to Unlock");
                    }
                }               
            }).focus(function(){
                $(".ui-datepicker-calendar").hide();
                $(this).attr("autocomplete","off");
            });
        });
    }

标签: javascriptjqueryhtmldatepickerjquery-ui-datepicker

解决方案


似乎 max Date 真的很重要。在这种特定情况下,我应该在 maxDate 的同一个月内有一个日期,但由于某种原因(我不记得并检查了程序其他部分的可能影响,但我找不到任何问题)我返回了第一个日期本月的。

所以这没有显示在日期选择器案例日历上,所以我不知道。这就是它覆盖 maxDate 的设置函数原因的方式。


推荐阅读