Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 multiple prevents other input from submitting form on enter

I have a form with a simple input and a select2 input like so : http://codepen.io/anon/pen/QyBxwE

Pressing enter while the first input is focused should submit the form (in this case, redirect to a 404 page).

For some reason, the multiple select2 input prevents the form submit. If I remove the select2 class or the multiple attribute, the form behaves normally.

Tested on Mac OS X Yosemite on Safari, Chrome and Firefox and it happens consistently on all browsers.

(I'm using jQuery 2.1.3 and select2 4.0.1)

like image 264
emi Avatar asked Feb 02 '16 16:02

emi


People also ask

How do I prevent enter from submitting the form?

Allow 'button' and 'submit' @NathanCH - This code does exactly the opposite from what you're saying. It only prevents enter from submitting the form and doesn't prevent any other functionality - for example, you might have a text field and want to use enter to create new paragraphs. There's a typo in the first line.

How to get multiple selected text in jQuery?

We will get multiple selected text in jquery. Jquery select2 plugin is a very famous jquery plugin, using select2 plugin we can do several thing like select box with search, select option with check box, ajax auto-complete etc. Here I will give you solution and full example for get select2 multiple selected value or text using jquery.

How to allow Enter key on Submit button in HTML?

If you'd like to allow enter key on submit buttons <input|button type="submit"> too, then you can always refine the selector as below. $ (document).on ("keydown", ":input:not (textarea):not (:submit)", function (event) { // ... });

Can I submit multiple forms at the same time?

Submit Multiple form is not supported, please check the response from CarlosFigueira in the following thread: Currently this scenario will not work if you have both forms set to the same data source (it should work if you have it set to different data sources, please let me know if this is not the case).


1 Answers

How forms work is that when you click enter, it has some default operations that it tries to preform. Step one is to submit the form. This only works if there is no other JavaScript preventing the form from submitting. Select2 multiple gets rid of the default operation by preventing default. That way when you start typing and click enter, it doesn't automatically submit.

The solution is to use the second type of default option. That is to have a submit button on the page. The form with go to the nearest submit button and click that. This operation isn't overridden by select2 JavaScript.

http://codepen.io/anon/pen/yeqxQJ

just add <input type="submit"> to your form, and it should work the way you want it to.

like image 110
Roger Avatar answered Oct 12 '22 14:10

Roger