如何使用数组中的值更新文本框值?

时间:2016-07-19 22:03:20

标签: javascript geolocation coordinates getelementbyid

我想使用JavaScript来使用数组中的值更新文本框值:

if (navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(function(position) {

        var pos = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };

        alert(pos['lat'] + ', ' + pos['lng']);

        data = pos['lat'] + ', ' + pos['lng'];

        document.getElementById("address").value = data.value;

    }, function() {

    });
}

我的问题是警报正确显示了lat和long坐标,但文本框值更新为“undefined”,这意味着data.value未定义。

我该如何解决这个问题?谢谢!

  • 注意:我似乎无法在stackoverflow上提供地理位置的代码片段吗? *

1 个答案:

答案 0 :(得分:0)

试试这个:     document.getElementById(" address")。value = data;