【发布时间】:2015-01-23 19:01:18
【问题描述】:
PrimeFaces 禁用按 Enter 键提交。
我正在运行在 WildFly 8.2 Final 上运行的 PrimeFaces 5.1。
我有一个对话框,有两个 inputNumbers 和两个按钮。第一个 inputNumber 对 ajax 模糊事件进行了一些计算。旁边是在 bean 中进行一些计算的按钮。问题是,当用户在焦点位于 inputNumber 时按下回车键时,按钮的动作会被触发,这真的很烦人。有没有办法通过对话框中的输入键禁用提交?
这是一个可以模拟我的行为的小 xhtml 对话框:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions" >
<p:dialog id="id_example" header="Test dialog"
widgetVar="exampleDialog" modal="true" closable="true" >
<h:form id="id_example_form">
<p:panelGrid columns="3" styleClass="noBorders">
<h:outputText value="Input 1:" />
<pe:inputNumber id="Input1" value="#{exampleBean.number1}">
<p:ajax event="blur" update="valueInput1" />
</pe:inputNumber>
<p:commandButton value="Check something else" action="#{exampleBean.checkForUsername()}"
update=":growl_form" />
<h:outputText value="Input 1:" />
<p:inputText id="valueInput1" value="#{exampleBean.number1}" />
<p:commandButton value="Save" action="#{exampleBean.save()}" oncomplete="PF('exampleDialog').hide();"
update=":growl_form" />
</p:panelGrid>
</h:form>
</p:dialog>
</ui:composition>
还有豆子:
package si.pucko.beans;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import si.pucko.util.Util;
@Named(value = "exampleBean")
@ViewScoped
public class ExampleBean implements Serializable {
private static final long serialVersionUID = 1L;
private BigDecimal number1;
public ExampleBean() {
number1 = new BigDecimal(BigInteger.ONE);
}
public BigDecimal getNumber1() {
return number1;
}
public void setNumber1(BigDecimal number1) {
this.number1 = number1;
}
public void checkForUsername() {
Util.ShowWarning("Just testing");
}
public void save() {
Util.ShowWarning("Saved");
}
}
问题是我无法禁用输入键:
<h:form onkeypress="if (event.keyCode == 13) { return false; }">
因为客户要求热键支持,并且输入用于提交表单,在某些情况下重新计算一些其他值等等......
【问题讨论】:
标签: ajax jsf jsf-2 primefaces