2017-07-02 16 views
0

我需要使用Geoposition API將追加到表單的用戶座標,但我無法理解如何等待發送表單之前獲得的位置。GeoPosition和jQuery表格

我有這種形式 編輯

現在我想在提交成功的回調形式...沒辦法...

<form id="coordForm" method="POST" action=""> 
<input type="hidden" id="latitude" name="latitude" /> 
<input type="hidden" id="longitude" name="longitude" /> 
<input type="text" name="name" id="name> 
//other input 

<button id="sumbit">Submit</button> 
          </form> 
<div id="location-lat-long"></div> 

那時,我正試圖獲得位置用jQuery,但它提交表單,而無需等待座標

$(document).ready(function() { 

    $('#submit').on('click', function(e){ 
     e.preventDefault(); 
     $("#location-lat-long").html("Finding location. Please wait..."); 
     // check if browser supports the geolocation api 
     if(navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(success); 

     } else { 
      $("#location-lat-long").val('Your browser doesn\'t support the geolocation api.'); 
     } 


    }); 



function errorCallback() {} 

function success(position){ 
    $("#location-lat-long").html(""); 
    $("#latitude").html(position.coords.latitude); 
    $("#longitude").html(position.coords.longitude); 
    $('#coordForm').submit(); 

} 


}); 

最後,表單不會提交...

回答

0

我會回答我的問題只是爲了幫助別人,因爲我花了幾個小時......

從來就不曾而且從不打電話給你的元素提交或其他敏感詞...