【问题标题】:JSF commandButton executed upon Enter on random form field在随机表单字段上输入时执行的 JSF commandButton
【发布时间】:2014-10-09 09:01:15
【问题描述】:

我有一个带有多个按钮的表单和一个显示“删除用户数据”的链接。单击链接后,会在执行任何操作之前显示确认弹出窗口。这是代码:

<p:confirmDialog closable="false"
    header="#{bundle['remove.user.data']}"
    message="#{bundle['remove.user.data.text']}"
    severity="alert" widgetVar="deleteUserPopup">
    <h:commandButton
        actionListener="#{userBean.deleteUserData}"
        value="#{bundle['yes']}" immediate="true"
        onclick="deleteUserPopup.hide()">
            <f:attribute name="userId"
                value="#{userBean.user.id}" />
            <f:ajax execute="@this" render="@all" />
    </h:commandButton>      
    <h:commandButton value="#{bundle['no']}"
        onclick="deleteUserPopup.hide()" type="button" />
</p:confirmDialog>

<div>
    <h:commandLink>
        <h:outputText value="#{bundle['remove.user']}" />
        <h:graphicImage name="remove.png" library="img"
            title="#{bundle['remove.user']}" />
        <f:ajax event="click"
            onevent="deleteUserPopup.show()" />
    </h:commandLink>
</div>

在表单中,我有很多字段,但是有问题的是文本字段,因为当用户在其中输入文本并按Enter键时,会立即调用方法deleteUserData无需确认弹出)。下面是一个两个字段的代码,例如:

<div>
    <h:panelGrid columns="2">
        <h:column>
            <h:inputText
                value="#{userBean.user.lastName}" id="userLastName" />
            <h:message for="userLastName" styleClass="warning" />
        </h:column>
        <h:column>
            <h:inputText
                value="#{userBean.user.firstName}" id="userFirstName" />
                <h:message for="userFirstName" styleClass="warning" />
        </h:column>
    </h:panelGrid>
</div>

以上所有代码(2 个 div 和一个确认对话框)都在 &lt;h:form&gt; 内 所以,我不明白,为什么在 Enter 上执行命令按钮的动作监听器?

【问题讨论】:

    标签: jsf input actionlistener enter commandbutton


    【解决方案1】:

    这是正常的 HTML 行为,一般来说,相对于当前输入元素的 firstnext input type="submit" 将在回车时被调用。此页面上的完整答案:Default action to execute when pressing enter in a form

    【讨论】:

    • 您好,感谢您的建议。不幸的是,建议的解决方案都没有奏效——既没有 JS 捕获回车键,也没有交换 HTML 中的按钮,甚至没有添加具有可见性的新第一个按钮:隐藏。我认为这里的问题不在于提交了 firstnext 输入,而是提交了另一个输入。您能建议另一种解决方法吗?
    • 你试过这个 吗?您可以使用带有 id 的 p:defaultCommand 到您的 Button 来定义按下 Enter 键时会发生什么
    • 可能是简单的解决方法:
    • 还有一些带有富人面孔示例的页面achorniy.wordpress.com/2010/11/06/…
    猜你喜欢
    • 1970-01-01
    • 2012-09-07
    • 2013-10-10
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-26
    • 2018-05-12
    相关资源
    最近更新 更多