简体   繁体   中英

How do I prevent the Enter button from running its default behavior in Firefox 12?

I have a form built in ASP.NET. The first control allows the user to choose a person from an auto-complete list. When the user pressed enter it would refresh the page and duplicate any information acquired through this first control. I am trying to remove the capability of the enter key from refreshing the page. This code all works in Chrome and IE7/8/9 (Don't care about 6). All I NEED is the return false for it to work in all browsers we support besides Firefox. The .click() is a bonus to add a bit of usability back to the key (so that it will activate the controls and check or uncheck check boxes, etc.)

None of this works in Firefox 12. The click occurs (proof that the code is reached when I want it) but the page refreshes every single time.

The focusNextInputfield() was one of the suggestions from a similar question and didn't do anything I wanted. It may have done what it was intended for but I can't tell because the page refreshed.

I found preventDefault() and stopPropagation() from yet another similar question on my own and it did nothing in FF.

I have even tried returning true for the heck of it.

   $(document).keydown(function (event) {
   //handles what happens when the user hits enter
       if (document.activeElement.nodeName !== 'TEXTAREA') {
           if (event.keyCode === 13 || event.which === 13) {
                $(document.activeElement).click();
               // $(document.activeElement).focusNextInputField();
                event.preventDefault();
                event.stopPropagation();
                return false;
           }
       }
    }); 

I am just looking for any suggestions or news on any reason none of this has any effect in FireFox 12? And I know that the code is reached and it all runs properly without error and even with all the excess code it still runs properly in Chrome and IE 7/8/9 as I said.

And through an earlier iteration I tried forcing the submit button to be clicked but it still refreshed anyway and validated and was overall a bad user experience.

Looks like you are using jQuery so all you need is to preventDefault in form submit event.

$j("#form-id").submit(function(e) {
    e.preventDefault();
    ...
});

Or:

$j("#form-id").submit(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM