簡體   English   中英

jquery,谷歌地圖地理編碼后提交表單

[英]jquery, submit form after google maps geocoding

我有一個收集人員地址的表格。 當他們點擊提交時,我想對他們的地址進行地理編碼,並使用它來填寫 lat 和 lng 字段。 這是我現在正在做的事情,它基於這里的第二個答案, 提交表單不會在 jquery ajax 調用中停止

<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>

和表格

<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>

lat 和 lng 字段填寫得很好,但表單實際上並沒有通過 $('#theform').submit() 提交。 要實際提交表單,您必須再次單擊提交。

關於我做錯了什么的任何想法? 如果有人問我是否想提交表單,即使地理編碼不成功,因為在這種情況下,我將使用 geopy 對地址服務器端進行地理編碼。

編輯:好的,所以我做了一個非常簡單的例子,根本無法提交工作:

<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>

我希望單擊提交鏈接時,您會像單擊提交按鈕時一樣收到一個警告框,但事實並非如此。 我提交有什么問題?

我剛剛想通了。 我不小心從我發布的表單中省略了我的提交按鈕。 它被命名為提交。 顯然,jquery 搞砸了(在此感謝: jQuery 不提交表格)。 我發布的簡單示例仍然無法正常工作,我不確定為什么,但我的實際問題已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM