简体   繁体   English

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

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

I have a form built in ASP.NET. 我有一个用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. 当用户按下Enter键时,它将刷新页面并复制通过第一个控件获取的任何信息。 I am trying to remove the capability of the enter key from refreshing the page. 我试图从刷新页面中删除enter键的功能。 This code all works in Chrome and IE7/8/9 (Don't care about 6). 此代码均适用于Chrome和IE7 / 8/9(不关心6)。 All I NEED is the return false for it to work in all browsers we support besides Firefox. 我需要的只是返回false,因为它可以在我们支持的除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.) .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. 这些都不适用于Firefox 12.点击发生(证明我希望它到达代码)但页面每次都刷新。

The focusNextInputfield() was one of the suggestions from a similar question and didn't do anything I wanted. focusNextInputfield()是来自类似问题的建议之一,并没有做我想做的任何事情。 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. 我发现了自己的另一个类似问题的preventDefault()和stopPropagation(),它在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? 我只是在寻找任何有关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. 而且我知道代码已经到达并且它都可以正常运行而且没有错误,即使使用了所有多余的代码,它仍然可以在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. 通过较早的迭代,我尝试强制单击提交按钮,但仍然会刷新并验证,总体上是一个糟糕的用户体验。

Looks like you are using jQuery so all you need is to preventDefault in form submit event. 看起来你正在使用jQuery所以你只需要在表单提交事件中防止默认。

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

Or: 要么:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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