问题描述
如何在 jquery 中引用 JSF 组件,因为我不知道要引用的组件的 id 前面的 id?
How do I refer to JSF components in jquery, as I dont know the id(s) prepended to the id of component I want to refer ?
推荐答案
你可以给所有 NamingContainer
视图中的组件如、
、
,
一个固定的 ID,以便可以安全地预测 HTML 生成的客户端 ID.如果您不确定,只需在网络浏览器中打开页面,右键单击并执行查看源代码.如果您在客户端 ID 链中看到像
j_id123
这样的自动生成的 ID,那么您需要为 那个 组件提供一个固定的 ID.
You can give all NamingContainer
components in the view such as <h:form>
, <h:dataTable>
, <ui:repeat>
, <my:composite>
a fixed ID so that the HTML-generated client ID is safely predictable. If you're uncertain, just open the page in webbrowser, rightclick and do View Source. If you see an autogenerated ID like j_id123
in the client ID chain, then you need to give exactly that component a fixed ID.
或者,您可以使用 #{component.clientId}
让 EL 将组件的客户端 ID 打印到生成的 HTML 输出中.您可以使用组件的 binding
属性将组件绑定到视图.像这样:
Alternatively, you can use #{component.clientId}
to let EL print the component's client ID to the generated HTML output. You can use component's binding
attribute to bind the component to the view. Something like this:
<h:inputText binding="#{foo}" />
<script>
var $foo = $("[id='#{foo.clientId}']"); // jQuery will then escape ":".
// ...
</script>
这只要求脚本是视图文件的一部分.
This only requires that the script is part of the view file.
作为另一种选择,你可以给有问题的元素一个类名:
As another alternative, you could give the element in question a classname:
<h:inputText styleClass="foo" />
<script>
var $foo = $(".foo");
// ...
</script>
作为另一种选择,您可以将 HTML DOM 元素本身传递给函数:
As again another alternative, you could just pass the HTML DOM element itself into the function:
<h:inputText onclick="foo(this)" />
<script>
function foo(element) {
var $element = $(element);
// ...
}
</script>
这篇关于如何在 jquery 中引用 JSF 组件 Id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!