I have a form which collects a persons address. When they click submit I want to geocode their address, and use that to fill lat and lng fields. Here is what I am doing now which is based on the 2nd answer here submit form does not stop in jquery ajax call
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
$(document).ready(function() {
var geocoder;
var address;
$('#theform').submit(function() {
geocoder = new google.maps.Geocoder();
address = $('#id_street').val() + " " + $('#id_street2').val() + " " + $('#id_city').val() + ", " + $('#id_state').val() + " " + $('#id_zip').val();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
$('#id_lat').val(results[0].geometry.location.lat().toFixed(6));
$('#id_lng').val(results[0].geometry.location.lng().toFixed(6));
$('#theform').unbind('submit');
$('#theform').submit();
} else {
$('#theform').unbind('submit');
$('#theform').submit();
}
});
return false;
});
});
</script>
and the form
<form method="POST" id="theform" action="">
<li><label for="id_street">Street</label> <input id="id_street" type="text" name="street" value="3520 Lebon Dr." maxlength="100" /></li>
<li><label for="id_street2">Street2 (optional)</label> <input id="id_street2" type="text" name="street2" maxlength="100" /></li>
<li><label for="id_city">City</label> <input id="id_city" type="text" name="city" value="San Diego" maxlength="50" /></li>
<li><label for="id_state">State</label> <select name="state" id="id_state">
...a bunch of state options...
<li><label for="id_zip">Zip Code</label> <input id="id_zip" type="text" name="zip" value="92122" maxlength="30" /></li>
<label for="id_lat">Latitude</label><input type="text" name="lat" value="0.000000" id="id_lat" />
<label for="id_lng">Longitude</label><input type="text" name="lng" value="0.000000" id="id_lng" />
</form>
The lat and lng fields get filled fine, but the form does not actually get submitted by $('#theform').submit(). To actually submit the form you have to click submit again.
Any ideas about what I am doing wrong? And in case anyone asks I do want to submit the form even if geocoding is unsuccessful as in that case I will be using geopy to geocode the address server side.
edit: Ok so I made a really simple example and can't get submit to work at all:
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#subby').click(function() {
$('#theform').submit();
});
});
</script>
</head>
<body>
<a href="#" id="subby">Submit</a>
<form id="#theform" action="javascript:alert('success!');">
<input type="submit" />
</form>
</body>
</html>
I would expect that when clicking the link Submit you would get an alertbox just like when you click the submit button, but this is not the case. What am I doing wrong with submit?
I just figured it out. I accidentally omitted my submit button from the form I posted. It was named submit. Apparently that mucks things up for jquery (thanks here: jQuery doesn't submit a form ). The simple example I posted still isn't working and I'm not sure why but my actual problem is solved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.