问题
I am using http://ubilabs.github.com/geocomplete/ to have a form submit latitude & longitude. If I just do the trigger below it works and fills up lat & lng fields. However, when I add the submit jquery code, it submits blank fields. I know its some timing thing since I can see the fields fill up but not sent via POST. But I can't figure it out, any suggestions?
<script>
$(document).ready(function(){
// Area for any jQuery
$("#geocomplete").geocomplete({
details: "form",
types: ["geocode", "establishment"]
});
$("#geoform").submit(function() {
$("#geocomplete").trigger("geocode");
alert('Handler for .submit() called.');
return true;
});
});
</script>
<form id="geoform" method=post action="/foo.php">
<input id="geocomplete" type="text" placeholder="Type in an address" value="Empire State Bldg" />
<!--<input id="find" type="button" value="find" />-->
<input type="submit" value="Go" />
<fieldset>
<input name="lat" type="text" value="">
<input name="lng" type="text" value="">
<input name="query" type="hidden" value="true">
</fieldset>
</form>
回答1:
I removed the trigger from the submit function and I added a delay.
<script>
$(document).ready(function(){
// Area for any jQuery
$("#geocomplete").geocomplete({
details: "form",
types: ["geocode", "establishment"]
});
$("#submit-button").click(function() {
$("#geocomplete").trigger("geocode");
setTimeout("$('#geoform').submit();",500);
});
});
</script>
<form id="geoform" method=post action="/foo.php">
<input id="geocomplete" type="text" placeholder="Type in an address" value="Empire State Bldg" />
<!--<input id="find" type="button" value="find" />-->
<input id="#submit-button" type="button" value="Go" />
<fieldset>
<input name="lat" type="text" value="">
<input name="lng" type="text" value="">
<input name="query" type="hidden" value="true">
</fieldset>
</form>
来源:https://stackoverflow.com/questions/15697140/form-submit-with-geocomplete-jquery-geocoding-doesnt-post-latitude-longitude