【问题标题】:SetDate on jquery Datepicker onClose does not change valuejQuery Datepicker onClose 上的 SetDate 不会改变值
【发布时间】:2020-02-21 16:02:57
【问题描述】:

我有一张桌子,每一行都有一个日期选择器。 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");
            });
        });
    }

【问题讨论】:

    标签: javascript jquery html datepicker jquery-ui-datepicker


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-19
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-29
      • 2017-04-29
      相关资源
      最近更新 更多