简体   繁体   English

如何从JSF 2.0中的javascript获取元素

[英]How to get element from javascript in JSF 2.0

I have JSF code like: 我有JSF代码,如:

<h:inputText id="from" value="#{fromToMBean.fromName}"/>

I would like to get this element from JavaScript by ID ( from ), but I can't, because in generated HTML it is j_idt8:from 我希望通过ID( from )从JavaScript中获取此元素,但我不能,因为在生成的HTML中它是j_idt8:from

How can I get this element in eg jQuery? 我怎样才能在例如jQuery中获得这个元素? Is there any way to force JSF2 not to change ids? 有没有办法强迫JSF2不要改变ID?

Is there any way to force JSF2 not to change ids? 有没有办法强迫JSF2不要改变ID?

You can set prependId="false" This way in generated HTML it will be from in place of j_idt8:from . 您可以设置prependId="false"这种方式生成的HTML这将是地方j_idt8:从

prependId : Flag indicating whether or not this form should prepend its id to its descendent's id during the clientId generation process. prependId:指示在clientId生成过程中此表单是否应将其id添加到其后代的id的标志。 If this flag is not set, the default value is true. 如果未设置此标志,则默认值为true。

How can I get this element in eg jQuery? 我怎样才能在例如jQuery中获得这个元素?

You can use ancestorComponent:from in jQuery to get this element. 您可以在jQuery中使用ancestorComponent:from来获取此元素。

Actually j_idt8 in j_idt8:from is auto generated id of ancestor component of your <h:inputText/> 实际上j_idt8中的j_idt8:from<h:inputText/>的祖先组件的自动生成的id

for example 例如

<h:form id="form">
<h:inputText id="from" value="#{fromToMBean.fromName}"/>
</h:form>

Now generated id of input text would be form:from 现在生成的输入文本的id将是form:from

If you don't provide id to a component than your browser generates that dynamically. 如果您没有为组件提供id,而不是浏览器动态生成它。 So don't forget to provide ids to components. 所以不要忘记为组件提供ID。

您可以使用仅为此元素指定的自定义类,并使用css选择器或为表单指定一个id,并获取具有id formid:from的元素formid:from

You can use jquery. 你可以使用jquery。 Simply, use a selector defining the text it should contains. 简单地说,使用一个选择器来定义它应该包含的文本。 Something like this: 像这样的东西:

$( "input[name*='from']" )

'*=' is used to say that the name attribute contains some string. '* ='用于表示name属性包含一些字符串。 Also there exist '~=' with similar meaning. 还存在具有相似含义的'〜='

For detailed explanations and examples visit http://api.jquery.com/attribute-contains-selector/ 有关详细说明和示例,请访问http://api.jquery.com/attribute-contains-selector/

In JSF 1.2 you can use forceId="true". 在JSF 1.2中,您可以使用forceId =“true”。 I'm not sure if you can use t:input in JSF 2, but you should be able to. 我不确定你是否可以在JSF 2中使用t:input,但你应该能够。 Then it's ID in HTML will be what you expect. 然后它的HTML中的ID将是您所期望的。

In order to achieve full ID for a component, use EL implicit objects and their properties such as #{cc.clientId} and #{component.clientId} . 为了获得组件的完整ID,请使用EL隐式对象及其属性,例如#{cc.clientId}#{component.clientId} Source: Acquire full prefix for a component clientId inside naming containers with JSF 2.0 . 来源: 使用JSF 2.0获取命名容器内的组件clientId的完整前缀

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM