我遇到了一些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()
}
});
});
});
請閱讀[我網站上的某些內容不起作用。我可以只粘貼一個鏈接嗎?](http://meta.stackexchange.com/questions/125997/something-on-my-web-site-doesnt-work-can-i-just-paste-a-link -to-它) – Quentin
還應閱讀[該HTML5佔位符屬性不是爲標籤元件的替代物(http://www.456bereastreet.com/archive/201204/the_html5_placeholder_attribute_is_not_a_substitute_for_the_label_element/) – Quentin
注意到和編輯,謝謝。 – Jimmy