I need to run some jquery code before submitting a form and there seem to be some issues.
Just hear me out before you rush to recommend this:
$(function() {
$('form').submit( function() {
/* some code */
return true;
});
});
I need , after pressing the "submit" button first calculate the longtitude and latitude variables of an address using google API and then submit the form. That requires listening for a response in a callback function.
The code I have so far looks like this:
$("form input:submit").click(function(e){
e.preventDefault();
var address = $(this).find('.address').val();
geocoder.geocode( { 'address': unescape(address)}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
$(".latSelector input:hidden").val(results[0].geometry.location.lat());
$(".lngSelector input:hidden").val(results[0].geometry.location.lng());
$("form").submit();
}
});
});
If I do the same using the "submit" function, the form submits before the callback function is triggered and the code behind executes with missing values.
If I use the "click" event on the button like the code above and submit manually, then the code executes correctly but the submit doesn't trigger the code behind of the form.
I think I am close to getting this to work but I can't find the magic combination that will run the code, process the callback values and then submit the form correctly, so that the code behind executes.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…