在JSF表单中按Enter键时指定默认a4j:commandButton

ahm*_*l88 1 javascript java jsf richfaces

我的JSF表单包含
h:inputText a4j:commandButton
而且
我有2个按钮(搜索)和(复位)

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>
Run Code Online (Sandbox Code Playgroud)

问题是,当我press Enter在编辑期间,任何表格中h:inputText 的第一个a4j:commandButton将被触发而不是(搜索)按钮

注意
我检查了答案
在表单中按Enter键时要执行的默认操作
但是h:commandButton
我面对的是JavaScript错误异常

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

McD*_*ell 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>
Run Code Online (Sandbox Code Playgroud)