[英]Jquery form submission not being triggered
我是 JavaScript 的新手。 我一直在嘗試設計一些代碼,當點擊搜索按鈕時對位置進行地理編碼,如果成功則提交表單。 為了使它稍微復雜一些,如果選擇了自動建議中的一個選項,它甚至在搜索按鈕被點擊之前也會對其進行地理編碼。
這一切似乎都有效,只是表單從未提交,我不知道為什么。
鏈接: 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()
}
});
});
});
你在打電話:
processLocation(function (success) {
但是您的 processLocation 在第二個參數上有回調:
function processLocation(query, callback)
嘗試從 processLocation 中刪除查詢參數:
function processLocation(callback)
或者用一個空白參數調用它:
processLocation(null, function (success)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.