gpt4 book ai didi

javascript - 将 JavaScript 数据结果放入表单输入值

转载 作者:行者123 更新时间:2023-11-28 08:50:47 27 4
gpt4 key购买 nike

好的,所以我有了在 onClick 之后根据地理位置生成纬度和经度的脚本。我现在希望将这些坐标放置在 onClick 之后的用户表单值字段内,而不是innerHTML。

我该如何去做呢?我还需要使用innerHTML吗?

我的表单详细信息:

<body>
<h1>Insert New Address</h1>
<form id="myForm" method="post" action="index3.php">
<input type="hidden" name="submitted" value="true">
<fieldset>
<legend>New Cafes</legend>
<label>Name<input type="text" name="name"></label>
<label>Address<input type="text" name="address"></label>
<label>Postcode<input type="text" name="address2"></label>
<label>Latitude<input type="text" name="lat" value="" id="addLat"></label>
<label>Longitude<input type="text" name="lng" value="" id="addLng"></label>
</fieldset>
<input type="submit" name="sub" value="Submit">
</form>


<p id="demo">Click the button to get your coordinates:</p>
<button onClick="getLocation()">Try It</button>

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

重申一下,我希望生成的纬度和经度结果自动放置在 for 中的空“值”字段中,这样用户就不必手动放置在框中。

非常感谢任何帮助:)

最佳答案

获取元素并添加值?

var x = document.getElementById("demo");

function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}

function showPosition(position) {
document.getElementById('addLat').value = position.coords.latitude;
document.getElementById('addLng').value = position.coords.longitude;
}

FIDDLE

关于javascript - 将 JavaScript 数据结果放入表单输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19161082/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com