我一直在試圖設計一個javascript了一個多星期(我是新來的JS),當搜索按鈕被打和的提交,如果成功的形式進行地理編碼的位置。爲了使它稍微複雜一點,如果選擇了autosuggest選項,它甚至在搜索按鈕被擊中之前也對其進行地理編碼。Jquery的表單提交未觸發
這一切似乎工作,但形式從未提交和我的生活中,我想不通爲什麼。如果任何人都可以發現我出錯的地方,我會非常感激,我已經花了很長時間在這個上面,現在我真的被卡住了。
鏈接:http://jsfiddle.net/sR4GR/42/
$(function() {
var input = $("#loc"),
lat = $("#lat"),
lng = $("#lng"),
lastQuery = null,
lastResult = null, // new!
autocomplete;
function processLocation(query, 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()
}
});
});
});
有你的console.log()的任何地方,看看究竟在何處你的代碼休息? – djowinz