I do not use forms and instead of buttons I use decorated css links. There is a question of usability when, for example, it is necessary to enter a login and password in a modal window and not transferring focus from input by pressing enter to send data for validation.

The question of how it is more convenient to implement, given the fact that there will be many such sets in the form of inputs and default links. For example, if you assign the same classes to a set, then you need to write n-handlers to n-sets. And I would like one.

UPD So far, the option has come to my mind to catch input with focus and look for the sister element of the link button with the default class.

Minus: it is necessary that they be "brothers" or at least have a common parent, which is not always convenient.

UPD So far, what is the solution

    4 answers 4

    $('input, textarea').live('keyup', function(e){ if( (this.nodeName == 'input' && e.keyCode == 13) || (this.nodeName == 'textarea' && e.keyCode == 13 && e.ctrlKey) ){ $(this).parents('form').eq(0).submit(); } }); 

    It catches inputs and textarea in forms. Textarea is sent only when ctrl is pressed.

    • I agree, it is absolutely unclear why to hang the handler for everything = \ - Zowie
    • With $('input, textarea') completely agree. But $(this).parents('form').eq(0).submit(); it seems that it’s not at all what the vehicle needs:> I don’t use forms and instead of buttons I use decorated css links . - Ilya Pirogov
    • This is the religious preferences of the TCA and IMHO, there is no winnings and, respectively, the same - Zowie
    • > not at all what you need TS Oops missed. = (And then I don’t understand how the TS sends the form. Where does the action come from? - ling
    • > And then I do not understand how the TS sends the form. Where does action come from? As far as I understand, an event that is hung on any link should simply be called. - Ilya Pirogov

    So far it turned out like this:

      $("*").keypress(function(event) { if ( event.which == 13 && !(event.ctrlKey)) { $("input:focus").parent().find(".default").click(); $("textarea:focus").parent().find(".default").click(); } }); //Переопределяет enter для textarea на Ctrl+enter $("*").keypress(function(event) { if(event.ctrlKey && event.which==13) $("textarea:focus").val($("textarea:focus").val()+"\n"); }); 
    • 2
      Not a bad decision, but there are a couple of notes: 1. Instead of $("input:focus") and $("textarea:focus") it would be better to use $(this) 2. Besides ctrlKey, for good, you also need to check altKey and shiftKey, since shift + enter is also sometimes used to wrap a line. 3. So you need to call event.preventDefault() to cancel the default event. - Ilya Pirogov
    • Thank you very much - I will consider. But with the third option a little incomprehensible. What is the default event for textarea - is a line break by pressing enter? And in general a little example would not hurt. - culebre
    • > What is the default event for textarea - is a line break by pressing enter? In textarea, this is a line break, while in input, the current form is sent. Now I will outline an example. - Ilya Pirogov
     $('input, textarea').live('keydown', function(e) { if (e.which != 13 || e.ctrlKey || e.shiftKey || e.altKey) { return; } e.preventDefault(); $(this).parent('.container').find('.default').click(); }); 

    Where .container is the parent of your "form".

    And instead of a crutch with the addition of ' \n ' by ctrl + enter, I would recommend using shift + enter, then no additional manipulations will be required.

    • Yes, actually, he did. - culebre

    Create a hidden element with the type of submit and do not suffer.

     <input type="submit" style="display: none" />