2012-02-16 35 views
1

我试图使用JavaScript来访问JSF的outputText价值的价值,但它抛出undefined.Below调用的访问JSF的outputText(标签)元素是代码JavaScript是无法通过inspite ID

<ui:composition template="/template.xhtml"> 
      <ui:define name="head"> 
       <script type="text/javascript"> 
        function sendDetails() 
        { 
         alert("am inside js"); 
         var key=document.getElementById('editForm:key').value; 
         alert(key); 
        } 
       </script> 
      </ui:define> 
    <ui:define name="body"> 
       <f:view> 
        <h:form id="editForm"> 
         <h:outputLabel id="key" value="#{editController.details.clientId}" style="font-weight: bold" > 
<h:commandLink id="analytics" onclick="sendDetails()" value="View"></h:commandLink> 

    </h:form> 
    </f:view> 
    </ui:define> 
    </ui:composition> 

当我点击命令链接它抛出undefined!我不能够访问的输出值label.I检查的萤火查看页面源

<label id="editForm:key" style="font-weight: bold"> 

1e20bb95-753e-4252-b6d6-e109fa07171e

这似乎与right..I检查console.log(document.getElementById('editForm:key'))console.log(document.getElementById('editForm:key').value)这表明<label id="editForm:key"> undefined

如何访问标签/ JSF的outputText value.I需要在JavaScript的解决方案只

回答

4

你不能得到h:outputLabel值与.value,因为它被呈现为HTML label标记,并且您无法获得h:outputText值与.value,因为它被呈现为HTML span标记。

使用.innerHTML代替两个标签。

你可以用Firebug找到它的所有属性。收藏此观看

document.getElementById('editForm:key') 

例如,这里是保存它的文本

.innerHTML 
.outerText 
.textContent 
+0

无法得到它与innerHTML的或outertext工作,但用的textContent工作完美...谢谢多个属性:) – enthusiastic 2012-02-16 11:38:05