如何使用 JSF 显示/隐藏组件? [英] How can I show/hide component with JSF?

查看:19
本文介绍了如何使用 JSF 显示/隐藏组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 JSF 显示/隐藏组件?我目前正在尝试在 javascript 的帮助下做同样的事情,但没有成功.我不能使用任何第三方库.

How can I show/hide component with JSF? I am currently trying so do the same with the help of javascript but not successfull. I cannot use any third party libraries.

谢谢|阿比

推荐答案

一般来说,你需要通过它的 clientId.此示例使用 JSF2 Facelets 视图和请求范围绑定来获取其他控件的句柄:

Generally, you need to get a handle to the control via its clientId. This example uses a JSF2 Facelets view with a request-scope binding to get a handle to the other control:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
  <h:head><title>Show/Hide</title></h:head>
  <h:body>
    <h:form>
      <h:button value="toggle"
               onclick="toggle('#{requestScope.foo.clientId}'); return false;" />
      <h:inputText binding="#{requestScope.foo}" id="x" style="display: block" />
    </h:form>
    <script type="text/javascript">
      function toggle(id) {
        var element = document.getElementById(id);
        if(element.style.display == 'block') {
          element.style.display = 'none';
        } else {
          element.style.display = 'block'
        }
      }
    </script>
  </h:body>
</html>

具体的操作方式取决于您使用的 JSF 版本.有关较旧的 JSF 版本,请参阅此博客文章:JSF:使用组件标识符.

Exactly how you do this will depend on the version of JSF you're working on. See this blog post for older JSF versions: JSF: working with component identifiers.

这篇关于如何使用 JSF 显示/隐藏组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆