【问题标题】:Javascript is unable to access jsf outputText(label) element inspite of calling via id尽管通过 id 调用,Javascript 无法访问 jsf outputText(label) 元素
【发布时间】:2012-03-07 14:02:30
【问题描述】:

我正在尝试使用 javascript 访问 jsf outputText 值的值,但它抛出 undefined。下面是代码

    <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>

当我单击命令链接时,它会抛出未定义!我无法访问输出标签的值。我在 firebug 中检查了查看页面源

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

1e20bb95-753e-4252-b6d6-e109fa07171e

这似乎是正确的..我检查了console.log(document.getElementById('editForm:key'))console.log(document.getElementById('editForm:key').value),显示&lt;label id="editForm:key"&gt; undefined

如何访问 label/jsf outputtext 值。我只需要 javascript 中的解决方案

【问题讨论】:

    标签: javascript html jsf-2


    【解决方案1】:

    你不能用.value得到h:outputLabel值,因为它被渲染成HTML label标签,你不能用.value得到h:outputText值,因为它被渲染成HTML @987654326 @标签。

    两个标签都使用.innerHTML

    您可以使用 Firebug 找到它的所有属性。添加此观看

    document.getElementById('editForm:key')
    

    例如,这里有更多包含其文本的属性

    .innerHTML
    .outerText
    .textContent
    

    【讨论】:

    • 无法让它与 innerHTML 或 outertext 一起使用,但与 textContent 完美搭配...谢谢:)
    猜你喜欢
    • 2011-09-28
    • 2019-11-08
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多