PrimeFaces命令按钮单击焦点指定id

时间:2015-04-29 06:31:43

标签: jsf-2 primefaces

我按下commandbutton需要焦点指定id不能在primefaces中工作。 我在下面的代码中尝试这个:

<h:form id="mainForm">
    <p:inputText id="userLoginNameTextId"/>
    <p:inputText id="passwordTextId"/>
     <p:commandButton id="userAddButton" value="#{platform.addButton}"
     onfocus="userLoginNameTextId" action="user.addAction"/>
</h:form>

注意:我在onfocus中尝试了以下属性

  1. onfocus="#mainForm:userLoginNameTextId"
  2. onfocus="#mainForm:userLoginNameTextId"
  3. onfocus="document.getElementId(mainForm:userLoginNameTextId)"

2 个答案:

答案 0 :(得分:0)

如果您关注命令按钮(例如执行一些javascript),onfocus将会触发。它不是用于将焦点设置为其他东西的东西。

单击按钮时,您是否真的需要将焦点设置为输入客户端?

答案 1 :(得分:0)

使用p:focus

尝试此操作
<p:panel id="addPanel">
            <p:focus context="addPanel" />
            <h:panelGrid columns="3" cellpadding="9" cellspacing="9">
                <f:facet name="header">
                    <p:messages style="text-align:left;" styleClass="messages"
                        errorClass="invalid" infoClass="valid" warnClass="warning"
                        globalOnly="true" />
                </f:facet>

                <p:outputLabel for="typeName" value="Type Name:" />
                <p:inputText id="typeName" required="true" />
                <p:message for="typeName" errorClass="invalid" />

                <p:outputLabel for="typeValue" value="Type Name:" />
                <p:inputText id="typeValue" required="true" />
                <p:message for="typeValue" errorClass="invalid" />

                <p:spacer></p:spacer>
                <h:panelGrid columns="1" id="addTypeAction">
                    <p:commandButton id="addButton" value="Save" styleClass="register"
                        update="@form" />
                </h:panelGrid>
                <p:spacer></p:spacer>
            </h:panelGrid>
        </p:panel>