问题
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