01/02/2012

Javascript e catch del tasto "enter"

Ecco una semplice funzione Javascript che consente di ottenere l'invio del contenuto di un campo di input semplicemente pigiando il tasto enter:

<script type="text/javascript">
  function onEnter(evt, frm)
  {
      var keyCode = null;
   
    if( evt.which )
    {
         keyCode = evt.which;
     }
    else if( evt.keyCode )
    {
         keyCode = evt.keyCode;
     }
    
    if( 13 == keyCode )
    {
         document.searchForm.searchbutton.click();
         return false;
     }
   
     return true;
  }
  </script>

javascript11.jpg

Il codice HTML ad essa associato è il seguente:

<form id="searchForm" name="searchForm" class="searchform">
                <input id="input" name="input" type="text" value="Search..." onFocus="if (this.value == 'Search...') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'Search...';}" onKeyPress="return onEnter(event,this.form)"/>
                <input id="searchbutton" name="searchbutton" class="searchbutton" type="button" value="a" onClick="document.forms['searchForm'].submit()"/>
</form>

Alla prossima.

11:00 Scritto da: nazarenolatella in Web Editing | Link permanente | Commenti (0) | Segnala | Tag: javascript, enter, keypress, submit, form | OKNOtizie |  Facebook