-2

我有以下表单,我只想用我的 p:commandButton 提交,但是,我在所有输入框中都收到了提交,我如何只在 p:commandButton 上提交表单?

<h:form>
            <p:panel header="Lista de Referências" id="panel">
                <h:panelGrid columns="2" cellpadding="5">

                    <h:outputLabel>Referência</h:outputLabel>
                    <p:inputText value="#{entradaProdutoController.referencia}" 
                                 onkeypress="if (event.keyCode == 13) {onchange(); return false; }"> 
                        <f:ajax event="change" 
                                render="textDescri" 
                                listener="#{entradaProdutoController.listener}"/> 
                    </p:inputText>

                    <h:outputLabel>Descrição</h:outputLabel>
                    <h:outputText id="textDescri" value="#{entradaProdutoController.replyWith}" /> 

                    <h:outputLabel>Quantidade</h:outputLabel>
                    <p:inputText size="5" value="#{entradaProdutoController.quantidade}" />

                </h:panelGrid>
            </p:panel>
            <p:commandButton value="Adicionar" update="printTable" actionListener="#{entradaProdutoController.addAction(event)}">
            </p:commandButton>
4

2 回答 2

2

我相信该onchange()方法会导致在按下 enter 时提交页面,如果您不希望这种行为尝试删除...

onkeypress="if (event.keyCode == 13) {onchange(); return false; }"
于 2012-08-22T17:50:41.040 回答
1

厌倦了上述建议而没有成功的结果,所以作为一种解决方法,这就是我正在做的,这可能不是最佳实践,但它有效(这样做的锤击方式):

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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"
      >
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>Adicionar Referências</h1>
        <h:form id="formID" onkeypress="if (event.keyCode == '13') {return false;}">
            <h:outputLabel>Referência</h:outputLabel>
            <h:inputText  id="referenciaLoookup" value="#{testController.referencia}"
                          onkeypress="if (event.keyCode == '13') {document.getElementById('formID:testeButton').click()}"
                          > 
            </h:inputText>

            <h:inputText value="#{testController.referencia}" 
                         onkeypress="if (event.keyCode == 13) { onchange(); return false; }">
                <f:ajax event="change" listener="#{testController.teste(event)}" />
            </h:inputText>


            <p:commandButton id="lookup" value="lookup"
                             actionListener="#{testController.listener(a)}"/>

            <p:commandButton id="adicionar" value="Adicionar" 
                             actionListener="#{testController.addAction(e)}">
            </p:commandButton>

            <p:commandButton id="testeButton" value="teste" style="visibility: hidden;"
                             actionListener="#{testController.teste(event)}"/>
        </h:form>
    </h:body>
</html>
于 2012-08-28T10:53:21.763 回答