获取纬度/经度按钮以填充输入框 [英] Get Latitude/Longitude Button to Fill Input Boxes

查看:163
本文介绍了获取纬度/经度按钮以填充输入框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框,我希望用户填充地理位置数据(经度和纬度)。我想要一个按钮,所以当用户点击它时,文本框会立即填充地理位置数据。

I have two text boxes that I want users to fill with geolocation data (latitude and longitude). I want to have a button, so when users click it, the text boxes are filled instantly with the geolocation data.

感谢w3schools,我可以将两个文本段落标签的位置,但我希望按钮填满文本框。这是我现在拥有的。任何人都有答案?

Thanks to w3schools, I can replace the text in two paragraph tags to the locations, but I'd like the button fill up the text boxes. Here's what I have now. Anyone have an answer?

<!DOCTYPE html>
<html>
<body>

<form>
Latitude: <input type="text" name="Latitude" /> <br />
Longitude: <input type="text" name="Longitude" />
</form>

<button onclick="getLocation()">Get Location</button><input type="submit" value="Submit" />

<p id="demo">Click the button to get your coordinates:</p>
<p id="demo2">Click the button to get your coordinates:</p>

<script>
var x=document.getElementById("demo");
var y=document.getElementById("demo2");
function getLocation()
  {
  if (navigator.geolocation)
    {
    navigator.geolocation.getCurrentPosition(showPosition);
    }
  else{x.innerHTML="Geolocation is not supported by this browser.";}
  }
function showPosition(position)
  {
  x.innerHTML=position.coords.latitude;  
  y.innerHTML=position.coords.longitude;    
  }
</script>

</body>
</html>


推荐答案

你并不是真的在说什么问题,据我所知innerHTML只适用于FF而不适用于IE。试试:

You are not really saying what the problem is, but as far as I know innerHTML only works in FF and not IE. Try:

function showPosition(position) {
  x.value=position.coords.latitude;  
  y.value=position.coords.longitude;    
}

这篇关于获取纬度/经度按钮以填充输入框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆