【发布时间】:2011-05-18 13:16:16
【问题描述】:
问题: 有时你会想从 javascript 访问一个组件
getElementById,但是 id 是在 JSF 中动态生成的,所以你
需要一种获取对象ID的方法。我在下面回答您如何做到这一点。
原问题: 我想使用如下代码。如何在我的 Javascript 中引用 inputText JSF 组件?
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<head>
<title>Input Name Page</title>
<script type="javascript" >
function myFunc() {
// how can I get the contents of the inputText component below
alert("Your email address is: " + document.getElementById("emailAddress").value);
}
</script>
</head>
<h:body>
<f:view>
<h:form>
Please enter your email address:<br/>
<h:inputText id="emailAddresses" value="#{emailAddresses.emailAddressesStr}"/>
<h:commandButton onclick="myFunc()" action="results" value="Next"/>
</h:form>
</f:view>
</h:body>
</html>
更新:这篇帖子Client Identifiers in JSF2.0 讨论了使用如下技术:
<script type="javascript" >
function myFunc() {
alert("Your email address is: " + document.getElementById("#{myInptTxtId.clientId}").value);
}
</script>
<h:inputText id="myInptTxtId" value="backingBean.emailAddress"/>
<h:commandButton onclick="myFunc()" action="results" value="Next"/>
建议inputText 组件上的属性id
创建一个可以通过 EL 使用#{myInptTxtId} 访问的对象,
在上面的例子中。文章继续指出 JSF 2.0 添加了
UIComponent 类的零参数 getClientId() 方法。
从而允许建议 #{myInptTxtId.clientId} 构造
以上获取组件实际生成的id。
虽然在我的测试中这不起作用。其他人可以确认/否认。 下面建议的答案存在上述问题的缺点 技术不行。所以很高兴知道上述技术是否 确实有效。
【问题讨论】:
标签: javascript jsf