Richfaces - 通过 ID 获取元素 [英] Richfaces - Get Element By Id
问题描述
我们在我们的一个项目中使用 Richfaces.
We are using Richfaces in one of our projects.
我需要将元素动态地集中在某些用户操作上.这里的问题是我只有 id(JSF 特定的 id,我给元素.)
I need to focus the element dynamically on some user action. The issue here is I only have the id (JSF specific id that I give to element.)
例如:对于 h:inputText
我用它作为
For example: for h:inputText
I use it as
<h:inputText id="userNameInputBox" value="<<some binding>>/>
并且这个输入框嵌入在单独的表单中.当我在表单中包含输入框时,输入框的 id 将是: formName:userNameInputBox (<<
and this input box is embedded in separate form. When I include the input box in form, the id of the input box will be: formName:userNameInputBox (<<formName>>:<<elementId>>)
我将在多个 JSP 中重复使用输入框.这意味着,我不知道我将在其中包含输入框的父表单.
I will be re-using the input box in multiple JSPs. That means, I do not know the parent form in which I will include the input box.
现在,我需要 focus() 输入框.目前我无法执行此操作,因为我没有实际生成的 id (<
) 但我只有 JSF 特定的我给的id.
Now, I need to focus() the input box. Currently I am not able to do this as I do not have the actual generated id (<<formname>>:<<elementId>>
) but I only have the JSF specific id I gave.
如何使元素聚焦?
推荐答案
使用 RichFaces 标记函数 #{rich:clientId( JSF_ID )}
为组件获取适当的客户端 ID.如果需要DOM树里面的元素,可以直接用#{rich:element(JSF_ID)}
Use the RichFaces tag function #{rich:clientId( JSF_ID )}
to get the appropriate client id for a component. If you need the element inside the DOM tree, you can get it directly with #{rich:element( JSF_ID )}
示例:onclick="#{rich:element('userNameInputBox')}.focus()"
我相信您需要 RichFaces 3.2.0 或更高版本才能使用此功能.
I believe you need RichFaces 3.2.0 or higher for this to work.
这篇关于Richfaces - 通过 ID 获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!