我使用下面的代碼獲取我當前位置的 LAT 和 LNG,以將其顯示在輸入表單中,但由于某種原因,它沒有按需要顯示我的 LAT 和 LNG,而是會瞬間顯示 LAT 和 LNG 然后消失。我嘗試更改瀏覽器權限以允許位置,但仍然顯示一閃然后消失。有人可以幫忙嗎?下面是我的代碼:<form> <button onclick="onFormSubmit()">GET CURRENT LOCATION</button><script type="text/javascript">function onGeoSuccess (position) { var lat = position.coords.latitude; var lng = position.coords.longitude; // NEXT 2 LINES WILL SET VALUE OF RESPECTIVE INPUTS document.getElementById('lat').value = lat; document.getElementById('lng').value = lng; // MAKE API CALL HERE, OR ANY OTHER NEXT STEPS } function onGeoError (err) { console.error("Error while trying to get position", err); } function onFormSubmit () { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(onGeoSuccess, onGeoError); } else { alert('GeoLocation not supported or not allowed'); } }</script> <div class="form-group"> <input type="text" name="lat" class="form-control" id="lat" placeholder="Your Latitude" data-msg="Please enter your latitude"> <div class="validate"></div> </div><div class="form-group"> <input type="text" name="lng" class="form-control" id="lng" placeholder="Your Longitude" data-msg="Please enter your Longitude"> <div class="validate"></div> </div> <div class="form-group"> <input type="text" name="subject" class="form-control" id="contact-subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject"> <div class="validate"></div> </div></form>我只想在輸入表單上顯示 LAT 和 LNG 以及每個 ID,但我不知道做錯了什么。它沒有按預期顯示。
LAT 和 LNG 未顯示在 HTML 的輸入表單上
隔江千里
2023-10-14 15:51:55