将JS和Jquery数据放入输入文本中

时间:2014-02-27 19:54:59

标签: javascript jquery input geolocation

我发现这个函数在JS中获得地理定位

    if (navigator.geolocation) {
        alert('Browser support geolocation')
        navigator.geolocation.getCurrentPosition(showPosition);
    }
    else { alert('Browser does not support geolocation'); }

    function showPosition(position) {

        alert(position.coords.latitude + ":" + position.coords.longitude);
        document.title = position.coords.latitude + ":" + position.coords.longitude;

    }

和这个改变一个输入文本的值(在jquery中)

$(document).ready(function(){
  $('button').click(function(){
    $('#local').attr('value',c);
  });
});

现在,我如何将(position.coords.latitude)和(position.coords.longitude)放入输入文本(#local)

非常感谢,这将节省我的工作哈哈

3 个答案:

答案 0 :(得分:0)

  function showPosition(position) {

     $('#local').attr('value',position.coords.latitude + ":" + position.coords.longitude);
  }

答案 1 :(得分:0)

确保包含jquery框架,然后:

function showPosition(position) {
    $('#local').attr('value',position.coords.latitude + ":" + position.coords.longitude);
}

$(document).ready(function(){
    if (navigator.geolocation) {
        alert('Browser support geolocation')
        navigator.geolocation.getCurrentPosition(showPosition);
     }
     else { 
         alert('Browser does not support geolocation'); 
     }
});

答案 2 :(得分:0)

保持简单:

function showPosition(position) {
    $('#local').val(position.coords.latitude + ' : ' +  position.coords.longitude);
}

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(showPosition);
} else {
    alert('Browser does not support geolocation');
}

DEMO