2013-05-29 33 views
2

我遇到了一些JavaScript問題,我得到了幫助。這是錯誤我得到:未定義不是Jquery腳本中的函數

Uncaught TypeError: undefined is not a function 

這似乎是伴我行:

callback(lastResult); // send the result back 

奇怪的是代碼似乎預期工作,但我顯然不希望有我的代碼出錯。有人可以幫助告訴我我要去哪裏嗎?

的jsfiddle:http://jsfiddle.net/spadez/uaZkV/3/

全碼:

$(function() { 
    var input = $("#loc"), 
     lat = $("#lat"), 
     lng = $("#lng"), 
     lastQuery = null, 
     lastResult = null, // new! 
     autocomplete; 

    function processLocation(callback) { // accept a callback argument 
     var query = $.trim(input.val()), 
      geocoder; 

     // if query is empty or the same as last time... 
     if (!query || query == lastQuery) { 
      callback(lastResult); // send the same result as before 
      return; // and stop here 
     } 

     lastQuery = query; // store for next time 

     geocoder = new google.maps.Geocoder(); 
     geocoder.geocode({ 
      address: query 
     }, function (results, status) { 
      if (status === google.maps.GeocoderStatus.OK) { 
       lat.val(results[0].geometry.location.lat()); 
       lng.val(results[0].geometry.location.lng()); 
       lastResult = true; // success! 
      } else { 
       alert("Sorry - We couldn't find this location. Please try an alternative"); 
       lastResult = false; // failure! 
      } 
      callback(lastResult); // send the result back 
     }); 
    } 

    autocomplete = new google.maps.places.Autocomplete(input[0], { 
     types: ["geocode"], 
     componentRestrictions: { 
      country: "uk" 
     } 
    }); 

    google.maps.event.addListener(autocomplete, 'place_changed', processLocation); 

    $('#searchform').on('submit', function (event) { 
     var form = this; 

     event.preventDefault(); // stop the submission 

     processLocation(function (success) { 
      if (success) { // if the geocoding succeeded, submit the form 
       form.submit() 
      } 
     }); 

    }); 
}); 
+1

請閱讀[我網站上的某些內容不起作用。我可以只粘貼一個鏈接嗎?](http://meta.stackexchange.com/questions/125997/something-on-my-web-site-doesnt-work-can-i-just-paste-a-link -to-它) – Quentin

+0

還應閱讀[該HTML5佔位符屬性不是爲標籤元件的替代物(http://www.456bereastreet.com/archive/201204/the_html5_placeholder_attribute_is_not_a_substitute_for_the_label_element/) – Quentin

+0

注意到和編輯,謝謝。 – Jimmy

回答

2
google.maps.event.addListener(autocomplete, 'place_changed', processLocation); 

如果添加processLocation作爲一個地圖事件監聽器,它會變得沒有參數傳遞事件觸發時。 callback參數是undefined然後,並導致您的異常(儘管不會停止您的控制流,因爲它是最後一條語句)。

這意味着你將檢查實際callback功能是否傳遞之前調用它:

function processLocation(callback) { // accept a callback argument 
    var query = $.trim(input.val()), 
     geocoder; 

    // if query is empty or the same as last time... 
    if (!query || query == lastQuery) { 
     if (typeof callback=="function") 
      callback(lastResult); // send the same result as before 
     return; // and stop here 
    } 

    lastQuery = query; // store for next time 

    geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 
     address: query 
    }, function (results, status) { 
     lastResult = status === google.maps.GeocoderStatus.OK; 
     if (lastResult) { 
      lat.val(results[0].geometry.location.lat()); 
      lng.val(results[0].geometry.location.lng()); 
     } else { 
      alert("Sorry - We couldn't find this location. Please try an alternative"); 
     } 
     if (typeof callback=="function") 
      callback(lastResult); // send the result back 
    }); 
} 

如果你有callback多次調用,不想到處進行類型檢測,你也可以定義

function noOperation() {} 

和使用,作爲對callback默認值,如果沒有被傳遞:

function processLocation(callback) { // accept a callback argument 
    if (typeof callback != "function") 
     callback = jQuery.noop; // predefined by jQuery 
    … 
} 
+0

謝謝你看這個並解釋它。這是否按照您的建議實施? http://jsfiddle.net/spadez/uaZkV/11/ – Jimmy

2

documentationgoogle.maps.event.addListener意味着,當它調用回調函數(processLocation)它這樣做沒有任何參數。

callbackprocessLocation你的函數定義的第一個參數,所以它是undefined當由谷歌地圖代碼調用。

您嘗試將它作爲函數調用,而不進行測試,如果它是一個。

要麼刪除嘗試調用它,要麼先用typeof進行測試。

相關問題