views:

569

answers:

3

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>
A: 

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

Kon
Don't make it break completely when JS isn't available. Build on things that work. http://icant.co.uk/articles/pragmatic-progressive-enhancement/
David Dorward
To be fair, the OP is locked into a JS solution.
Paul Alan Taylor
+2  A: 

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')" /> 
Jojo
There is some mistake but your solution help me out.<input id="Text1" type="text" onkeypress="return submitOnEnter(event, 'p2.htm')" /> <input id="Text2" type="text" onkeypress="return submitOnEnter(event, 'p3.htm')" /> <input id="submit1" type="submit" value="submit" onclick="alert('no1')" />function submitOnEnter(e, page) { var key; if (window.event) key = window.event.keyCode; //IE else key = e.which; //Firefox return false; //prevent auto hit submit button }}
Mike108
Thank you very much.
Mike108
Yes, you're right I forgot the return.
Jojo
A: 
<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.

Vampired