javascript - 显示和隐藏功能在 JSF 中不起作用

标签 javascript jquery jsf

我正在尝试使用 JQuery 在 JSF 中隐藏和显示我的文本区域。但这不起作用。条件不错,但功能不行。 怎么了?我不是 JS 的新手,但我开始探索 JSF,并以非常奇怪的方式使用 JS。 您可以在下面看到我的代码。 感谢x

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:b="http://bootsfaces.net/ui"
    xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
<h:head>
    <title>Add new field</title>


</h:head>
<h:body>
    <script
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">

    </script>

    <h:form>
        <b:commandButton look="link" action="response?faces-redirect=true"
            value="Response" />
        <b:commandButton look="link" action="fields?faces-redirect=true"
            value="Fields" />
    </h:form>
    <h:form style="margin: 0 auto">
        <h3>Adding Form Components</h3>
        <h:panelGrid columns="1">
            <h:outputLabel id="label" for="label">Label:</h:outputLabel>
            <div>
                <b:inputText binding="#{infoManageBean.labelInput}"
                    placeholder="Name, Country, etc." required="true" id="labelText"
                    value="#{infoManageBean.field.label}">
                </b:inputText>
                <p:message for="labelText" />
                <h:outputText value="#{addFieldValidationBean.label}" />
                <br /> <br />
            </div>
            <h:outputLabel for="type">Type:</h:outputLabel>

            <b:selectOneMenu onchange="handleChange(this.value)" required="true"
                value="#{infoManageBean.field.type}">

                <f:selectItems value="#{fieldManageBean.getFieldTypes()}"
                    var="value" itemLabel="#{value}" itemValue="#{value}" />
            </b:selectOneMenu>
            <br />
            <br />


            <h:inputTextarea id="itemArea" cols="30" rows="10" />


            <h:outputLabel for="color">Required:</h:outputLabel>
            <b:selectBooleanCheckbox value="#{infoManageBean.field.required}">
            </b:selectBooleanCheckbox>


            <h:outputLabel for="color">Is active:</h:outputLabel>
            <b:selectBooleanCheckbox value="#{infoManageBean.field.isActive}" />
            <input type="hidden" name="method" value=" ADD " />
            <b:commandButton look="primary" binding="#{infoManageBean.button}"
                id="addFieldBtn" validateClient="true"
                action="#{infoManageBean.insertOrUpdateField()}"></b:commandButton>
        </h:panelGrid>
    </h:form>
    <script type="text/javascript">
        function handleChange(selection) {
            var COMBO_BOX = 'COMBO_BOX';
            if (selection == COMBO_BOX) {
                console.log('visible');
                $('#itemArea').show();
            } else {
                console.log('hidden');
                $('#itemArea').hide();
            }
        }
    </script>
</h:body>

</html>

最佳答案

默认情况下,JSF 表单会为其子 id 添加前缀,如下所示:“form-id:child-id”,因此 jquery 无法使用选择器“#child-id”找到该元素。

在表单中使用 prependId="false":

<h:form style="margin: 0 auto" prependId="false"> 

或者以以下形式定义 id:

<h:form style="margin: 0 auto" id="formId"> 

并使用不同的选择器:

$('#formId\\:itemArea')

关于javascript - 显示和隐藏功能在 JSF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43459151/

相关文章:

javascript - 如何在 Angular 服务中注入(inject)依赖项?

JavaScript 为什么 'this' 未正确绑定(bind)

javascript - 加载 jQuery 后如何运行 jQuery 函数?

带有 SelectItems 的 JSF 2.0 selctOneMenu

jsf - PrimeFaces 改变了我们引用 widget 变量的方式

javascript - 使用 JavaScript 添加删除类的最有效方法

javascript - JQuery 同一节点中最接近的元素

javascript - 附加对象而不是文本?

javascript - 处理从 PHP 页面返回的 MYSQL 查询结果

Spring JSF 集成纯 Java 配置(无 web.xml)