Javascript innerHTML转换为表单文本值

时间:2018-11-27 22:29:01

标签: javascript html

我有一个具有innerhtml函数的javascript代码,例如

bool enabled

我想输入上面的javascript代码的输出,并输入到输入文本表单值中

<script>
var x = document.getElementById("gps");

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 + 
    "," + position.coords.longitude;
}
</script>

但是我无法获得想要的结果,是否需要添加或更改代码?

1 个答案:

答案 0 :(得分:1)

您需要调用函数。

假设<script>块位于页面底部,<input>呈现后,这应该可以工作:

<script>
var x = document.getElementById("gps");

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 + 
    "," + position.coords.longitude;
}

getLocation();
</script>