Javascript 按回车键时如何禁用自动提交行为?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2020940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:25:49  来源:igfitidea点击:

How to disable auto submit behavior when hitting enter key?

javascriptbutton

提问by Mike108

I want to hit enter key to go to p2.htm or p3.htm according to the input text that I was typing in. And I also want to hit submit1 button to alert('no1') manually.

我想根据我输入的输入文本按回车键转到 p2.​​htm 或 p3.htm。我还想手动按 submit1 按钮来 alert('no1')。

It works in FireFox, but in IE6, when I hit enter key it will submit the submit button.

它适用于 FireFox,但在 IE6 中,当我按回车键时,它将提交提交按钮。

How can I make the thing right in IE 6 as it is in FireFox?

我怎样才能像在 FireFox 中一样在 IE 6 中做正确的事情?

I use javascript and jQuery.

我使用 javascript 和 jQuery。

<input id="Text2"  type="text"  onkeyup="if(event.keyCode==13){go2();}" /> 
<input id="Text3"  type="text"  onkeyup="if(event.keyCode==13){go3();}" /> 

<input id="submit1"  type="submit" value="submit" onclick="alert('no1')" />

<script type="text/javascript">
    function go2()
    {
        window.location = "p2.htm";
    }
    function go3()
    {
        window.location = "p3.htm";
    }
</script>

采纳答案by Jojo

This function should do the trick:

这个函数应该可以解决问题:

function submitOnEnter(e, page) {
    var key;

    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.which; //Firefox & others

    if(key == 13)
        window.location = page;
}

You should call it like this:

你应该这样称呼它:

<input id="Text2"  type="text"  onkeyup="submitOnEnter(event, 'p2.html')" /> 

回答by WEFX

If using MVC3, you can disable submitting via Enter by editing the BeginForm call as follows:

如果使用 MVC3,您可以通过编辑 BeginForm 调用来禁用通过 Enter 提交,如下所示:

@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))
{
    ...
}

回答by Jake Coxon

<form onsubmit="return false"></form>

This will stop the form proceeding to the next page after either by clicking a submit button or pressing enter.

单击提交按钮或按 Enter 键后,这将停止表单进入下一页。

回答by user3612690

The problem with the accepted solution is that normal submit buttons no longer work. You must script all the buttons.

已接受的解决方案的问题是普通的提交按钮不再起作用。您必须编写所有按钮的脚本。

<form onsubmit="return false"></form>

Here's a better solution that doesn't break non javascript submit buttons. This solution simply tells the browser to not do default behavior when a user hits the enter key on form inputs.

这是一个更好的解决方案,它不会破坏非 javascript 提交按钮。该解决方案只是告诉浏览器在用户点击表单输入上的 Enter 键时不要执行默认行为。

// prevent forms from auto submitting on all inputs
$(document).on("keydown", "input", function(e) {
  if (e.which==13) e.preventDefault();
});

回答by Kon

Change the input type from "submit" to "button"

将输入类型从“提交”更改为“按钮”

回答by suryaprakash pisay

<%@taglib uri="/struts-tags"  prefix="s" %>
<html>
<head>
    <script type="text/javascript">
        function stopsubmit()
        {
            document.ourform.onsubmit="return true";
        }
        function pup()
        {
            return true;
        }
    </script>
</head>
<body>
    <form action="sa.jsp" name="ourform" onsubmit="return false">
        <s:submit action="" onclick="stopsubmit()" theme="simple"></s:submit>
    </form>
</body>

回答by Sahi R. Repswal

You can do by using :

您可以使用:

<script type="text/javascript">
    $(document).ready(function() {
       $(".div/formClass").bind("keypress", function(e) {
          if (e.keyCode == 13) {
             return false;
          }
       });
    });
</script>