function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
GSPGSP 

Pressing Enter key then command button will execute

please help me...here is my code

 

<apex:page standardController="account"    extensions="enterkeyclass">
<apex:form >

   <script type='text/javascript'>
        function noenter(ev)  {
            if (window.event && window.event.keyCode == 13 || ev.which == 13) {
                doSearchAF();
                return false;
             } else {
                  return true;
             }
         }
          
    </script>

  <apex:pageBlock >
  <apex:messages />
  <apex:actionFunction name="doSearchAF" action="{!searchJobs}" />
    <apex:pageBlockButtons location="bottom">
         <apex:commandButton value="Search Jobs" action="{!searchJobs}" onkeypress="noenter(event);"/>
        
       </apex:pageBlockButtons>

  <apex:pageBlockSection >
 
  </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form>
</apex:page>

 

---------------------

public class enterkeyclass {
Apexpages.Message mess;
    public enterkeyclass(ApexPages.StandardController controller) {

    }
   
    public pagereference searchJobs(){
   
    mess = new Apexpages.Message( Apexpages.Severity.ERROR,'No Results Found');
    return null;
    }

}

gautam_singhgautam_singh

Hi,

Try this Piece of Code , This works correctly in my Developer Org. Please Hit Kudos if this provides some useful information and mark it as a Solution so that others with the same query could get the answers without paing much time.


// Controller 


public class enterkeyclass{
 
    
    public enterkeyclass(ApexPages.StandardController controller) {

    }
   
    public PageReference searchJobs(){
   
        ApexPages.Message mess = new ApexPages.Message(ApexPages.Severity.ERROR, 'This is Error!');
        system.debug('##' + mess  );
        ApexPages.addMessage(mess);
 
    return null;
    }

}



//Visualforce Page



<apex:page standardController="account" id="pg" extensions="enterkeyclass">
    <apex:pagemessages id="pm"/>
    <script type='text/javascript'>
        function noenter(ev)  {
           // alert('stop here');
            if (window.event && window.event.keyCode == 13 || ev.which == 13) {
                doSearchAF();
                return false;
             } else {
                  return true;
             }
         }
          
    </script>
    
    <script type="text/javascript">
    window.onload = setFocus
    function setFocus() {
    document.getElementById("pg:frm:pb:pBB:cmd").focus();
    }
    </script>
    
    <apex:form id="frm">
    <apex:actionFunction name="doSearchAF" action="{!searchJobs}" />
        <apex:pageBlock id="pb" >
     
                <apex:pageBlockButtons id="pBB" location="bottom">
                    <apex:commandButton value="Search Jobs" rerender="pm" id="cmd"  onkeypress="noenter(event);" action="{!searchJobs}" />
                </apex:pageBlockButtons>
                <apex:pageBlockSection >
                </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

Important :

Click on the Star Icon aside if this post provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You