5

I've blocked Enter (return) key, actually, transformed it in Tab key. So when pressed inside input text fields it acts as Tab key. This is good, but I need it to trigger the submit button when pressed in the last field, below is the code for the Enter key mutation:

            $('input').keydown(function(event) {
                if(event.which == 13) {
                    event.preventDefault();
                    $(this).nextAll('input:first').focus();
                }
            });

And the form code is a sequence of input type="text" and a button type="submit" at the end [Edited] Actually the code is this one, taken from jquery: Enter to Tab trigger in specific parts. But I don't know why it was working yesterday today is not working:

$(':text').keydown(function(e) {
    if(e.keyCode == 13 && $(this).attr('type') != 'submit') {
        e.preventDefault();
        $(this).nextAll('input:first').focus();
    }
});
Community
  • 1
  • 1
msmafra
  • 1,704
  • 3
  • 21
  • 34

2 Answers2

6

If you give your last input the class of last then simply modify your code to something like

$('input').keydown(function(event) {
            if(!$(this).hasClass("last")){
                if(event.which == 13) {
                    event.preventDefault();
                    $(this).nextAll('input:first').focus();
                }
            }
        });     
Tom Walters
  • 15,366
  • 7
  • 57
  • 74
  • Thanks man, but it seems there is something wrong here the code is not working with or without your changes. I don't know what I did wrong. – msmafra Apr 29 '11 at 17:18
  • Right, fiddled with the code a little - http://jsfiddle.net/zfBNE/ - does that help? – Tom Walters Apr 29 '11 at 17:24
  • The problem is that even the older code stopped working here. In jsfiddle is working fine – msmafra Apr 29 '11 at 17:27
  • How odd, maybe try some cave-main debugging with FireBug and console.log() to see where the code is dying? – Tom Walters Apr 29 '11 at 17:28
  • Hehe found the problem! I changed the labels. Was and I've changed to that got in my way. I'll test your code now. – msmafra Apr 29 '11 at 17:31
  • None of this will work on Mac - there's other key code for enter/return. – versedi Sep 09 '15 at 12:26
  • @versedi Just tested on Safari and `e.which` returns 13 as the key-code for the return key, what did you find? – Tom Walters Sep 12 '15 at 14:57
  • I've tested alot of solutions across multiple systems and in case of using Apple's Mac + Safari the behaviour has to be overriden by including key code 3 – the enter and return are considered separate keys while in Windows and Linux family they're separate keys, but generating same key code. I'm not sure if this is not model specific behaviour. Reference: http://www.monkeybreadsoftware.eu/listarchive-realbasic-getting-started/2006-02-24-2.shtml P.S. Even after adding '3' as keycode the overall behaviour is not as expetected – key needs to be pressed twice to have the Tab key effect. – versedi Sep 13 '15 at 22:02
-1
// Definir o que acontece quando o usuário pressiona ENTER
$('input, select, textarea').live('keydown', function(e) { // Para todos os campos do formulário
    if (e.which == 13) { // Se pressionou ENTER
        if (e.ctrlKey) { // Se pressionou CTRL
            $(this).closest('form').submit(); // Envia o formulário
        } else { // Se não
            var fields = $(this).closest('form').find('input, select, textarea'); // Criamos uma lista dos campos do formulário
            var total = fields.length; // Identificamos a quantidade de campos
            var index = fields.index(this); // Identificamos a posicao do campo atual
            fields // Da lista de campos, ...
                    .eq( // na posicao ...
                        index + // do campo atual + ...
                        (e.shiftKey // Pressionou a tecla SHIFT?
                            ? // Se pressionou ...
                                (index > 0 // A posição atual é maior que 0 (zero)?
                                    ? // Se for maior
                                        -1 // campo anterior
                                    : // Se não ...
                                        0 // Primeiro campo
                                )
                            : // Se não ...
                                (index < total // Posicao atual é menor que o total de campos?
                                    ? // Se for menor ...
                                        +1 // proximo campo
                                    : // Se não ...
                                        total // Último campo
                                )
                        )
                        // Neste momento ja encontramos o campo que deverá ser selecionado
                    ).focus(); // Selecionamos o campo
            return false; // Impedimos que a ação padrão seja executada (Envio do formulário)
        } // FIM - se não pressionou CTRL
    } // FIM - se pressionou ENTER
}); // FIM da função