【问题标题】:JSF composite component id for use in jQuery用于 jQuery 的 JSF 复合组件 ID
【发布时间】:2014-11-07 14:42:39
【问题描述】:

我想在jsf中编写一个复合组件,并通过jquery添加一些功能。

小例子: 我的复合组件定义如下所示:

[...]
<t:inputText
    id="#{cc.attrs.id}"
    value="#{cc.attrs.value}"
    required="false"/>

<h:panelGroup rendered="#{cc.attrs.required}">
    <script type="text/javascript">
        jQuery(document.getElementById("#{cc.attrs.id}")).addClass("mandatory")
    </script>
</h:panelGroup>
[...]

会产生这样的结果:

<input name="content_form:name:name" id="content_form:name:name" type="text" value="">
<script type="text/javascript">
    jQuery(document.getElementById("name")).addClass("mandatory")
</script>

如您所见,jquery 获取了错误的 id。发生这种情况是因为 #{cc.attrs.id} 的评估似乎不会发生在不同的点上。

如何获得可预测的 id 或者是否有解决方法?

提前致谢。

【问题讨论】:

    标签: jquery jsf composite-component


    【解决方案1】:

    在这种情况下,我找到了一个“可行的解决方案”:

    <script type="text/javascript">
        jQuery(document.getElementById("#{cc.clientId}:#{cc.attrs.id}")).addClass("mandatory")
    </script>
    

    将使用我的输入元素的“真实 ID”呈现。

    解释:

    cc.clientId 返回我们所在的复合组件的id,结果:("content_form:name")

    cc.attrs.id 会返回id属性的值,结果:("name")

    这可以连接到 "content_form:name:name" 并用作 jquery 的 ID。

    我知道这不是从输入标签“获取 id”,而是预测结果。

    它有效,但如果有人知道更好的方法 它,我很高兴听到。

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 2011-06-27
      • 2013-03-23
      • 2013-04-22
      • 1970-01-01
      • 2012-04-08
      相关资源
      最近更新 更多