簡體   English   中英

在以JSF形式按Enter時指定默認的a4j:commandButton

[英]Specifying default a4j:commandButton when press Enter in JSF form

我的JSF表單包含
多個 h:inputText多個 a4j:commandButton

我有2個按鈕(搜索)和(重置)

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

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

問題是,當我在編輯任何h:inputText的過程中press Enter時,將觸發表單中的第一個a4j:commandButton而不是(search)按鈕

注意
我檢查了答案
在表單中按Enter鍵時執行的默認操作
但是對於h:commandButton
而且我遇到了JavaScript錯誤異常

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

您必須將事件傳遞給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>

您可以使用richfaces的Hotkey 使用您的示例:

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

使用PrimeFaces組件:

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

將此功能與聚焦組件結合使用,您將大放異彩!

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM