Prevent enter key from triggering button

放肆的年华 提交于 2019-12-10 17:17:18

问题


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;

                }


来源:https://stackoverflow.com/questions/27240364/prevent-enter-key-from-triggering-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!