0
你好我試圖停止提交與jquery的過程,但它不工作。停止提交表單與jquery
以下是提交過程的當前代碼。當返回錯誤時不應該提交。
function validateinput(gu) {
var geocoder= new google.maps.Geocoder();
var address = $(gu).find("input[name=autocomplete]").val();
geocoder.geocode({ 'address': address,
componentRestrictions: {
country: 'DE'
}}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if(!results[1]){
$.each(results[0].address_components, function(){
if(this.types[0] == "street_number"){ street_number = this.long_name; }
if(this.types[0] == "route"){ route = this.long_name; }
if(this.types[0] == "administrative_area_level_1"){ administrative_area_level_1 = this.long_name; }
if(this.types[0] == "postal_code"){ postal_code = this.long_name; }
});
if(typeof(street_number) !== "undefined" && street_number !== null && street_number !== '') {
$(gu).find("input[name=street_number]").val(street_number);
$(gu).find("input[name=route]").val(route);
$(gu).find("input[name=administrative_area_level_1]").val(administrative_area_level_1);
$(gu).find("input[name=postal_code]").val(postal_code);
$(gu).find("input[name=lat]").val(results[0].geometry.location.lat());
$(gu).find("input[name=lng]").val(results[0].geometry.location.lng());
return true;
}else{
$(gu).find("input[name=autocomplete]").val(administrative_area_level_1 + ", " + route + " ");
$(gu).find("input[name=autocomplete]").focus();
$(gu).find('span.addr_error').html('Bitte gebe deine Hausnr. an.');
return false;
}
}else{
$(gu).find("input[name=autocomplete]").focus();
$(gu).find('span.addr_error').html('Bitte wähle deine Lieferadresse.');
return false;
}
}else {
$(gu).find("input[name=autocomplete]").focus();
$(gu).find('span.addr_error').html('Wir konnten deine Adresse nicht finden.');
return false;
});
}
$("#aadress-form").submit(function(){ return validateinput(this) });
$("#ladress-form").submit(function(){ return validateinput(this) });
如何停止提交表單?對不起,我的英語不好我希望你能理解我的問題,並會幫助我。
'geocoder.geocode()'方法是異步,您應該從'validateinput返回'FALSE'()'和僅在需要時提交'form',例如使用'gu.submit();'(以在你的代碼中返回true;') –
謝謝你完美的作品, –