【问题标题】:Why doesn't JavaScript set h:inputHidden value? [duplicate]为什么 JavaScript 不设置 h:inputHidden 值? [复制]
【发布时间】: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


【解决方案1】:

改变这个:

      var elem = document.getElementById('listPlacesForm:hiddenMapCenter')

到这里:

      var elem = document.getElementById('hiddenMapCenter')

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2011-10-28
  • 2023-04-07
相关资源
最近更新 更多