2012-08-22 30 views
-2

我有下面的表單,我只想用我的p:commandButton提交,但是,我在所有輸入框中提交,我如何僅在p:commandButton上提交表單?如何控制JSF表單提交? Primefaces 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> 

回答

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> 
2

我相信onchange()方法會導致網頁被按下時,輸入要提交,如果你不希望這樣的行爲嘗試刪除...

onkeypress="if (event.keyCode == 13) {onchange(); return false; }" 
+0

如果我刪除它,我也沒有辦法產品羅okup這是使用該代碼背後的全部想法,但是我不想將整個表單簡單地組裝在一起 – Astronaut