我收到语法错误。说我的代码可能无法工作,直到我修复此错误,但我没有看到任何错误。错误行指向此let weekAgo = new Date();
,但这对我来说是正确的。我忽略了什么吗?它也导致我的日期选择器中的文本"来自"和"到"不会自动显示。任何帮助将不胜感激。
该文字显示在my fiddle但由于某些原因不在我的申请中。但该应用程序抛出了语法错误。所以任何帮助都会非常好!
<script>
(function() {
//move these out since they don't need to
const today = new Date();
let weekAgo = new Date();
weekAgo.setDate(today.getDate() - 7);
const $from = $("#StartDate");
const $to = $("#EndDate");
$(function() { //DOM-loaded
//these don't change
const reportFields = $('#location, #locationbtns, #locationtextarea, #chosendates, #submitbtn, #formattype');
const employeeFields = $('#employeelist, #employeelistbtns, #employeelisttextarea');
const loc = $("#loc");
const EmployeeName = $("#EmployeeName");
const selectedElement = $('#selected');
const selected1Element = $('#selected1');
const reportType = $('#reporttype');
const generatereportform = $("form[name=generatereport]");
$(document).click(function(clickEvent) {
switch (clickEvent.target.id) {
case 'add':
setLocationOptionsSelected(true);
break;
case 'rem':
setLocationOptionsSelected(false);
break;
case 'add1':
setSelectedOnEmployeeOptions(true);
break;
case 'rem1':
setSelectedOnEmployeeOptions(false);
break;
}
});
$(document).change(function(changeEvent) {
switch (changeEvent.target.id) {
case 'reporttype':
handleReportTypeChange();
break;
case 'loc':
handleLocationChange();
break;
case 'EmployeeName':
handleEmployeeNameChange();
break;
}
});
function handleReportTypeChange() {
var value = reportType.val();
if (value === "checklistreports") {
generatereportform[0].reset();
reportFields.show();
loc.prop('required', true);
employeeFields.show();
generatereportform.attr("action", "index.cfm?content=reportsassociate");
EmployeeName.prop('required', true);
} else if (value === "locationreports") {
generatereportform[0].reset();
reportFields.show();
loc.prop('required', true);
employeeFields.hide();
generatereportform.attr("action", "index.cfm?content=reportslocation");
EmployeeName.prop('required', false);
} else {
generatereportform[0].reset();
reportFields.hide();
loc.prop('required', false);
employeeFields.hide();
generatereportform.attr("action", "#");
EmployeeName.prop('required', false);
}
resetDatePickers();
}
function dateSelectHandler(dateText) {
if (this.id == 'StartDate') {
$to.datepicker("option", "minDate", dateText);
} else {
$from.datepicker("option", "maxDate", dateText);
}
}
function resetDatePickers() {
let datePickerConfig = {
changeMonth: true,
maxDate: today,
onSelect: dateSelectHandler
};
$from.datepicker(datePickerConfig).datepicker('setDate', weekAgo);
$to.datepicker(Object.assign(datePickerConfig, {
minDate: weekAgo
})).datepicker('setDate', today);
}
function handleLocationChange() {
var selected = [];
loc.find("option:selected").each(function() {
selected.push($(this).text());
});
selectedElement.val(selected.join("\n"));
}
function setLocationOptionsSelected(selected) {
loc.find("option").prop("selected", selected);
loc.change();
}
function setSelectedOnEmployeeOptions(selected) {
EmployeeName.find("option").prop("selected", selected);
EmployeeName.change();
}
function handleEmployeeNameChange() {
var selected = [];
EmployeeName.find("option:selected").each(function() {
selected.push($(this).text());
});
selected1Element.val(selected.join("\n"));
}
});
})();
</script>
答案 0 :(得分:1)
Object.assign()(或者任何版本。请参阅MDN文档的Browser Compatibilty部分)
。解决方法是仅手动设置 minDate 属性:let toDatePickerConfig = datePickerConfig;
toDatePickerConfig.minDate = weekAgo;
$to.datepicker(toDatePickerConfig).datepicker('setDate', today);
见以下演示:
const today = new Date();
let weekAgo = new Date();
weekAgo.setDate(today.getDate() - 7);
$(function() {
const $from = $("#StartDate");
const $to = $("#EndDate");
function dateSelectHandler(dateText) {
if (this.id == 'StartDate') {
$to.datepicker("option", "minDate", dateText);
} else {
$from.datepicker("option", "maxDate", dateText);
}
}
let datePickerConfig = {
changeMonth: true,
maxDate: today,
onSelect: dateSelectHandler
};
$from.datepicker(datePickerConfig).datepicker('setDate', weekAgo);
let toDatePickerConfig = datePickerConfig;
toDatePickerConfig.minDate = weekAgo;
$to.datepicker(toDatePickerConfig).datepicker('setDate', today);
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<span class="srch_title" for="StartDate">From:</span>
<input type='text' name="StartDate" id="StartDate" value="" required/>
<span class="srch_title" for="EndDate">To:</span>
<input type='text' name="EndDate" id="EndDate" value="" required/>