jquery.typeahead matched result causes modal for to close and parent to reload on hitting the enter key

44 Views Asked by At

The modal forms in our app all close and reload the parent form when you hit the enter key. Not really causing an issue but can get round that with the code below:

            $(document).ready(function () {
                $(window).keydown(function (event) {
                    if (event.keyCode == 13) {
                        event.preventDefault();
                        return false;
                    }
                });
            });

Placing my cursor in any text input and pressing the enter key does nothing as expected, including the typeahead input element. When I type in some text which is NOT matched to any results into the typeahead element and hit the enter key then nothing happens to the form, but if I add text which IS matched (does not need to be fully matched or the item selected from the results) hitting the enter key reloads the parent page completely.

I hope I have described this issue ok and can confirm the code is run when hitting enter with matched text but it seems to ignore this.

Thanks in advance for your help and if I have missed any information you need to help please advise.

EDIT:

There is a first time for everything, managed to find the answer. There is a callback for the typeahead which controls a submit which I can just set:

            onSubmit: function (e) {
                return false;
                ..and anything else
            }

This now allows me to press the enter key when a matched or partially matched item is typed/selected and not submit the form.

1

There are 1 best solutions below

0
Derek Jee On

I answered and edited the question with the solution I found. Thanks for the views.