Prevent enter key from triggering button

2019-06-24 09:14发布

问题:

I have a search input box which when a user presses enter needs to do nothing. I am using EmberJS and Jquery with the below code. Currently it works to disable a pop up from being triggered but for some reason in IE9 when enter is pressed a toggle button becomes in focus. Works fine in Chrome. I've tried tabindex and preventDefault but neither do the trick.

   if ($el.hasClass('form-control')) {
                    if ( e.which == 13) {
                        this.get('controller').flipit();

                    }
                }

Thank you.

EDIT------

Here is a snippet of my page. When a user hit enter within the search box the button to the right is getting highlighted. How would I prevent this?

回答1:

Do this

$("#inputBox").on('keydown', function(event) {
    if (event.key == "Enter") event.preventDefault();
});


回答2:

The solution:

   if ($el.hasClass('form-control')) {
                    if (e.key == 13) {
                        this.get('controller').flipit();
                    }

                    e.stopPropagation();
                    e.preventDefault();
                    return false;

                }