숨겨진 입력 값 필드에 "경도, 위도"를 전달하고 싶습니다. 다른 접근 방식을 시도했지만 값이 채워지지 않습니다. 내가 가지고있는 코드는 다음과 같습니다.숨겨진 입력에 JS 변수 전달
<form class="myForm" action="includes/teste.php" method="POST" id="form-id">
<input name="myField" type="hidden" id="myField" class="myField" value="" />
</form>
<script language="JavaScript" type="text/javascript">
window.onload=function(){
if(navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else
{
alert("Geolocation is not supported by this browser.");
}
}
function showPosition(pos){
//document.write("Latitude: "+pos.coords.latitude+" Longitude: "+pos.coords.longitude);
//document.getElementById("hidden1").value = "Latitude: "+pos.coords.latitude+" Longitude: "+pos.coords.longitude;
document.getElementById('myField').value = (pos.coords.latitude+","+pos.coords.longitude);
}
document.getElementById("form-id").submit()
</script>
다시 편집 해 드리겠습니다.
콘솔에 오류가 있습니까? –
숨겨진 입력에 이름이 없으므로 양식을 제출할 때 서버로 전송되지 않습니다. – Barmar
숨겨진 필드가 값을 얻지 못한다는 것을 어떻게 알 수 있습니까? – stepanian