2014-01-16 104 views
0

我已經檢出了這個threadthis one但它並沒有真正幫助我。未捕獲ReferenceError:functionName未定義

我有一個ASP:複選框控件如下所示。

<asp:CheckBox ID="chbArchived" runat="server" Checked='<%# Bind("archived") %>' OnClick=changeExpirationDate() /> 

我的JavaScript是如下:

<script type="text/javascript"> 
     $(document).ready(function() { 
      //alert("got here"); 
      $('.insDateTime').datetimepicker({ 
       ampm: true 
      }); 
      changeExpirationDate(){ 
       var currentDate = new Date; 
       var futureDateTime = (currentDate.getMonth() + 4) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear() + ' ' + formatAMPM(currentDate); 
       var expiredDateTime = (currentDate.getMonth() + 1) + '/' + currentDate.getDate() + '/' + currentDate.getFullYear() + ' ' + formatAMPM(currentDate); 
       var archivedCheckbox = document.getElementById('cphContent_fmvNewsEventList_chbArchived').checked; 
       if(archivedCheckbox == true) 
       { 
        document.getElementById('cphContent_fmvNewsEventList_txtExpirationDate').value = expiredDateTime; 
       } 
       else{ 
        document.getElementById('cphContent_fmvNewsEventList_txtExpirationDate').value = futureDateTime; 
       } 
      }; 
      function formatAMPM(date) { 
       var hours = date.getHours(); 
       var minutes = date.getMinutes(); 
       var ampm = hours >= 12 ? 'pm' : 'am'; 
       hours = hours % 12; 
       hours = hours ? hours : 12; // the hour '0' should be '12' 
       minutes = minutes < 10 ? '0'+minutes : minutes; 
       var strTime = hours + ':' + minutes + ' ' + ampm; 
       return strTime; 
      }; 
     });   
    </script> 

的問題是我一直得到這個JavaScript錯誤「未捕獲的ReferenceError:changeExpirationDate沒有定義」當我點擊複選框。任何建議/幫助非常感謝。

回答

0
}); 
changeExpirationDate(){ <-- where is the function declaration? 
    var currentDate = new Date; 

又名

function changeExpirationDate() { 
+0

謝謝!這是一個非常愚蠢的錯誤 - 完全忘了。 – 2myCharlie

1

變化

changeExpirationDate(){ 

function changeExpirationDate(){ 

第一使它看起來像你想叫它;第二個定義它。

0

好的,再看一次後,我發現這是一個非常愚蠢的錯誤。我在函數名changeExpirationDate()之前忘了「函數」,因此它給了我這個錯誤。

+0

當然,但如果你在'.ready()'回調中聲明它,它仍然會失敗。指定爲HTML屬性的處理程序在全局範圍內執行。 –

相關問題