Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable auto submit behavior when hitting enter key?

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.

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

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

I use javascript and 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>
like image 913
Mike108 Avatar asked Jan 07 '10 14:01

Mike108


People also ask

How do I stop form submit on enter key press?

getElementById("testForm"); form. addEventListener("submit",function(e){e. preventDefault(); return false;}); This solution will now prevent the user from submit using the enter Key and will not reload the page, or take you to the top of the page, if your form is somewhere below.

How do I stop automatic form submission?

type="submit" is the default (as specified by the HTML spec): The missing value default and invalid value default are the Submit Button state. Show activity on this post. Also, if you use type="button" your form will not be submitted.


1 Answers

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

@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))
{
    ...
}
like image 111
WEFX Avatar answered Sep 30 '22 17:09

WEFX