开发者

jQuery hit enter to submit the current form on page with multiple forms?

I have a page with multiple forms and was wondering if there was any examples showing how to submit the current form you filled in when hitting ent开发者_如何学运维er?

Thanks


Submit the closest form

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

You have to add an event keydown and submit the closest 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 on jQuery docs


The default behavior of browsers, when hitting enter while inside a form element, is to submit the form that the element belongs to ..

So you have nothing to worry about, unless i am misunderstanding the question ..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜