2013-12-12 23 views
1

JSF形式包含
h:inputTexta4j:commandButton

我有2個按鈕(搜索)和(復位)指定默認A4J:在JSF形式的commandButton時按Enter

我需要當我編輯press Enter任何h:inputText
(搜索)按鈕將被觸發

<h:form id="searchForm" onkeypress="formKeypress();"> 

    <h:panelGrid columns="4" > 

     <h:outputText value="name" /> 
     <h:panelGroup> 
      <h:inputText 
       title="name" 
       value="#{myController.name}" >       
      </h:inputText> 
      <a4j:commandButton 
       title="name Detail" 
       action="#{myController.nameDetail}" >    
      </a4j:commandButton> 
     </h:panelGroup> 

     <h:outputText value="city" /> 
     <h:panelGroup> 
      <h:inputText 
       title="city" 
       value="#{myController.city}" >       
      </h:inputText> 
      <a4j:commandButton 
       title="name Detail" 
       action="#{myController.cityDetail}" >    
      </a4j:commandButton> 
     </h:panelGroup> 

    </h:panelGrid> 


    <h:panelGrid > 
     <h:panelGroup> 
      <a4j:commandButton 
       id="searchButton" 
       value="search" title="search" 
       action="#{myController.search}" 
       render="searchResultsGrid" />      
     </h:panelGroup> 
    </h:panelGrid> 


    <f:verbatim> 
     <script type="text/javascript" > 
      <!-- 

      function formKeypress() { 
      // if (event.keyCode == 13) { 
      //  document.getElementById('searchForm:searchButton').click(); 
      //  return false; 
      // } 
      } 

      --> 
     </script> 
    </f:verbatim>  

</h:form> 

問題是, 當我press Enter形式編輯任何h:inputText 第一a4j:commandButton期間將被解僱 不是(搜索)按鈕


我查了答案
Default action to execute when pressing enter in a form
但是,對於h:commandButton
而我面對的JavaScript錯誤異常

ReferenceError: event is not defined
[Break On This Error]
if (event.keyCode == 13) {

+0

好的,但CSS鏈接交換鏈接中的按鈕似乎是一個贏家,容易完成。 – Gimby

回答

6

您必須將事件傳遞給JavaScript函數:

<h:form onkeypress="return formKeypress(event);" 
     id="srch"> 
    <h:inputText /> 
    <h:commandButton id="no" value="NO!" action="#{bean.no}" /> 
    <h:commandButton id="yes" value="YES!" action="#{bean.yes}" /> 
</h:form> 
<script type="text/javascript"> 
    var formKeypress = function(event) { 
    if (event.keyCode === 13) { 
     document.getElementById('srch:yes').click(); 
     return false; 
    } 
    return true; 
    }; 
</script> 
0

您可以使用RichFaces' Hotkey。使用你的例子:

<rich:hotKey selector="#searchButton" key="return"> 
    <rich:componentControl target="searchButton" operation="click" /> 
</rich:hotKey> 
+0

我試過了,但它不工作! 也按下'enter'(name Detail)按鈕不是(搜索)按鈕 –

+0

@YajliMaclo我的錯誤,'enter'應該是'return' – kolossus

-1

使用PrimeFaces組件:結合

<!-- Default button when pressing enter --> 
<p:defaultCommand target="submit"/> 

使用此重點組成部分,你會搖滾!

<!-- Focus on first field, or first field with error --> 
<p:focus context="feesboek"/>