JQuery hit enter to submit current form to page with multiple forms?

I have a page with several forms, and I was wondering if there are any examples showing how to submit the current form that you filled out when you pressed the enter button?

thank

+3
source share
2 answers

Submit the closest form

$("element").closest("form").submit();

You need to add an event keydownand send the nearest form:

$("input[type=text]") // retrieve all inputs
    .keydown(function(e) { // bind keydown on all inputs
        if (e.keyCode == 13) // enter was pressed
            $(this).closest("form").submit(); // submit the current form
    });

See closest in jQuery docs

+8
source

The default behavior of browsers when entering a form element is to submit the form to which the element belongs.

So you have nothing to worry about if I do not understand the question.

0
source

Source: https://habr.com/ru/post/1768773/


All Articles