【发布时间】:2015-06-25 17:11:09
【问题描述】:
我看到了很多例子,也做了同样的事情,但我不能用 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>
【问题讨论】:
-
您是否尝试在
document.getElementById中使用hiddenMapCenter作为id 而不是listPlacesForm:hiddenMapCenter? -
您是否拥有多个具有相同 id 的元素?
-
好的,从那个 HTML 你会注意到元素的实际 id 是...?
-
@Juhana 实际 ID 是:
mainTabId:listPlacesForm:hiddenMapCenter:) -
好的。它适用于下一行:
var elem = document.getElementById('mainTabId:listPlacesForm:hiddenMapCenter');
标签: javascript jsf hidden-field