我有一张桌子,每一行都有一个日期选择器。 Datepickers仅选择月份,并且在关闭时我设置了每月的第一天,除非这一天在minDate之前,所以我想在那里设置minDate。
SetDate函数不起作用。我的日志告诉我,perfectDate的设置正确,但是当我在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");
});
});
}
您的代码似乎有效。我没有从选项中删除maxDate,因为我没有整个表格。您确定
scheduleTaskMinDate()
返回日期对象?
尝试这个小提琴。像minDate一样具有魅力。