如何防止Enter按钮在Firefox 12中运行其默认行为?

时间:2021-03-31 07:11:34

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.)

我在ASP.NET中创建了一个表单。第一个控件允许用户从自动完成列表中选择一个人。当用户按下enter时,它将刷新页面并复制通过第一个控件获得的任何信息。我正在尝试从刷新页面中删除enter键的功能。这段代码在Chrome和IE7/8/9中都可以运行(不关心6),我只需要返回false就可以在除Firefox外的所有浏览器中运行。.click()是一个额外的好处,可以为键添加一些可用性(这样它就可以激活控件,检查或取消复选框等)。

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.

这些在火狐12中都不适用。点击发生(证明代码在我需要时到达),但是页面每次都会刷新。

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.

focusNextInputfield()是来自类似问题的建议之一,我没有做任何我想做的事情。它可能已经完成了它的目的,但我不能说,因为页面刷新了。

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

我自己从另一个类似的问题中找到了preventDefault()和stopPropagation(),它在FF中没有任何作用。

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

我甚至还尝试过返回true。

   $(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.

我只是在寻找任何建议或新闻,任何理由,这些对FireFox 12没有任何影响?我知道代码已经到达,并且运行正常,没有错误,即使有多余的代码,它仍然可以在Chrome和IE 7/8/9中正常运行。

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.

在之前的迭代中,我尝试强制单击submit按钮,但它仍然刷新并进行验证,这是一个糟糕的用户体验。

1 个解决方案

#1


2  

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

看起来您正在使用jQuery,所以您只需要在表单提交事件中使用preventDefault即可。

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

Or:

或者:

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

#1


2  

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

看起来您正在使用jQuery,所以您只需要在表单提交事件中使用preventDefault即可。

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

Or:

或者:

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