Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
OnKeyPress on Input
Message
De
12/03/2012 14:39:59
James Blackburn
Qualty Design Systems, Inc.
Kuna, Idaho, États-Unis
 
 
À
12/03/2012 08:42:30
Information générale
Forum:
Javascript
Catégorie:
Autre
Divers
Thread ID:
01538034
Message ID:
01538098
Vues:
30
This is easy if you use jquery.
$(document).delegate(':input','keypress',function(e){
   if (this.id == 'whatever') {
      document.forms[0].submit(); // or something like that.
    }
})
>By default, if you have an input field with a Input Type=Submit button, this will make it a post if you hit Enter in an input field of the form. This works ok for as long as the submission control is as indicated. However, if you have a graphical toolbar for the Save/Cancel, for example, in order to simulate the same, we have to use the OnKeyPress event of an input field and assign a javascript function which will determine if the Enter key was press so to submit the form.
>
>Instead of adding this OnKeyPress event to every input field, would there be a way to have it generic? I can assign document.onkeypress=FormKeyPress, which will be the javascript function. But, this will also trigger a call to this function whenever an Enter key is pressed, even if this is in a textarea field. In that case, it should not post the form. So, based on that approach, the one which adds an event at the form level, is there a way to detect that the click was done in an input field or a textarea field so to avoid doing anything if the click was done in a textarea field?
>
>Note that onkeypress is recommanded to be replaced by onkeydown so it will work with all browsers as some of them intercept the ESC key, such as Chrome, and they will not hit the event.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform