我看到了很多例子并做了同样的事情,但我无法用JavaScript设置价值。
有我的代码
<h:form id="listPlacesForm">
<h:inputHidden id="hiddenMapCenter" value="#{userPlaces.center}" />
</h:form>
和JavaScript
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(findPosition);
} else
error('Geo Location is not supported');
function findPosition(position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
alert('Start')
var str = lat + ", " + lng;
var elem = document.getElementById('listPlacesForm:hiddenMapCenter')
alert(str)
elem.value = 'asfsf'
alert('It works!' + str);
}
除了行elem.value = 'asfsf'
之外,所有这些代码都有效
我尝试过:elem.value = 'sfd'
,elem.value = "asfd"
,elem.value = str
有什么想法吗?
更新1
生成的HTML
<form id="mainTabId:listPlacesForm"
name="mainTabId:listPlacesForm" method="post"
action="/DarkComm/app/settings?execution=e3s1" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="mainTabId:listPlacesForm" value="mainTabId:listPlacesForm" />
<input id="mainTabId:listPlacesForm:hiddenMapCenter"
type="hidden"
name="mainTabId:listPlacesForm:hiddenMapCenter" value="" />
<input type="hidden" name="javax.faces.ViewState"
id="javax.faces.ViewState" value="e3s1" />
</form>
答案 0 :(得分:-2)
改变这个:
var elem = document.getElementById('listPlacesForm:hiddenMapCenter')
对此:
var elem = document.getElementById('hiddenMapCenter')