2017-09-25 58 views
1

我收到語法錯誤。說我的代碼可能無法正常工作,直到我解決這個錯誤,但我沒有看到任何錯誤。錯誤行指向這個let weekAgo = new Date();,但是這對我來說很合適。我可以忽略一些東西嗎這也會導致我的日期選擇器中的文本「from」和「to」不能自動顯示。任何幫助將不勝感激。JS函數語法與datepicker錯誤

該文本顯示在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

你所提到的行語法錯誤點,但語法錯誤實際上說了什麼? –

+0

所有的dreamweaver所說的是該行的語法錯誤。 「代碼提示可能無法正常工作,直到你解決這個錯誤。」 –

+0

'const today = new Date();'你在哪個瀏覽器上運行,。 'const'是ES6。 – Keith

回答

1

Object.assign()不被IE 11 支持(或與此有關的任何版本。請參閱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/>

+0

想看看這個嗎? https://codereview.stackexchange.com/questions/176527/js-function-warning-before-closing-browser我正在尋找寫在我的環境中的最佳方式 –