使用jQuery mobile将表单文本输入设置为地理位置纬度和经度

时间:2013-06-28 00:12:44

标签: jquery-mobile

我正在尝试创建一个多页移动应用程序,希望只有两页,并希望将地理位置纬度和经度放入两个单独的表单文本输入中。我希望这可以通过按钮按下或点击。

我真的不知道如何:

  1. 定位表单文本输入字段
  2. 编写功能
  3. 这是我到目前为止所做的:

    <div data-role="fieldcontain">
        <label for="GPS Location">GPS Lat</label>
        <input name="GPS Lat" id="GPSlat" value="" type="text">
    </div>
    <div data-role="fieldcontain">
        <label for="GPS Location">GPS Long</label>
        <input name="GPS Long" id="GPSlong" value="" type="text">
    </div>
    
    <div data-role="content">
        <script type="text/javascript">
            navigator.geolocation.getCurrentPosition(function(position)
            { 
                var lat = pos.coordinates.latitude;
                var lng = pos.coordinates.longitude;
                console.log( lat + ":" + lng);
            });
        </script>
    </div>
    
    <input value="SetGPS" id="SetGPS" data-inline="true" type="submit">
    <br>
    <div data-role="content">
        <script type="text/javascript">  
            $("#SetGPS").click(function(){
            $("#GPSlat input").text(lat);
            $("#GPSlong input").text(lng)
        </script>
    </div>          
    

1 个答案:

答案 0 :(得分:0)

您刚刚提出了一些名称和语法错误:

  • 您为getCurrentPosition所做的功能采用了一个名为position的参数,但您在其中引用了pos
  • latlng在上述函数中定义,因此click函数无法使用它们
  • 您引用coordinates,但字段为is actually called coords
  • 您正在设置输入字段的文本,但您应该设置值
  • 您的输入选择器不正确。它们应该是input#id
  • 您的点击功能缺少关闭大括号和括号。

以下是您的代码应该是什么样的:

var lat, lng;

navigator.geolocation.getCurrentPosition(function(position)
{ 
    lat = position.coords.latitude;
    lng = position.coords.longitude;
    console.log( lat + ":" + lng);
});

// ...

$("#SetGPS").click(function()
{
    $("input#GPSlat").val(lat);
    $("input#GPSlong").val(lng);
});
相关问题