2012-02-16 80 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