使用h:selectBooleanCheckbox显示/隐藏其他输入字段 [英] Show/hide another input fields using h:selectBooleanCheckbox

查看:305
本文介绍了使用h:selectBooleanCheckbox显示/隐藏其他输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF2和Java来构建Web应用程序.我想构建如下图所示的表单:

I am using JSF2 and Java to build a web application. I want to build a form like the one at the picture below:

当有人取消选中该复选框时,该表格应消失:

When somebody unchecks the checkbox the form should disappear:

此处是gwt的示例.

到目前为止, 我在ManagedBean中尝试了带有<f:ajax>标记和PropertyActionListener的东西,但是没有用.有人可以给我一个例子或至少一些提示来实现我的目标.我真的会很感激

So far, I tried some stuff with the <f:ajax> tag and an PropertyActionListener in the managedBean but it didn't work. Can somebody give me an example or at least some hints to accomplish my goal. I would be really thankfull

推荐答案

在复选框中使用<f:ajax render="idOfPanelContainingInputFields">,并为包含输入字段的组件提供一个rendered属性,该属性取决于复选框的状态.无需其他JS代码.

Use <f:ajax render="idOfPanelContainingInputFields"> in the checkbox and give the component containing the input fields a rendered attribute which depends on the checkbox's state. No need for another JS code clutter.

<h:form>
    <fieldset>
        <legend>
            <h:selectBooleanCheckbox binding="#{showUserInfo}">
                <f:ajax render="idOfPanelContainingTextBox" />
            </h:selectBooleanCheckbox>
            <h:outputText value="User information" />
        </legend>
        <h:panelGroup id="idOfPanelContainingTextBox" layout="block">
            <ui:fragment rendered="#{not empty showUserInfo.value and showUserInfo.value}">
                <p>
                    <h:outputLabel for="firstName" value="First name:" />
                    <h:inputText id="firstName" value="#{bean.user.firstName}" />
                </p>
            </ui:fragment>
        </h:panelGroup>
    </fieldset>
</h:form>

上面的示例将复选框绑定到视图,您当然也可以将其绑定到boolean bean属性,然后可以从rendered属性中删除not empty支票.

The above example binds the checkbox to the view, you can of course also bind it to a boolean bean property, you can then remove the not empty check from the rendered attribute.

            <h:selectBooleanCheckbox value="#{bean.showUserInfo}">

...

            <ui:fragment rendered="#{bean.showUserInfo}">

另请参见:

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