按下Enter键时如何禁用自动提交行为?
问题描述:
我想按Enter键根据输入的文本转到p2.htm或p3.htm。我还想按一下Submit1按钮来手动提示('no1')。
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.
它可以在FireFox中使用,但是在IE6中,当我按Enter键时,它将提交提交按钮。
It works in FireFox, but in IE6, when I hit enter key it will submit the submit button.
如何我将IE 6中的内容正确设置为FireFox中的内容吗?
How can I make the thing right in IE 6 as it is in FireFox?
我使用JavaScript和jQuery。
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>
答
此功能应该可以解决问题:
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;
}
您应该这样称呼:
<input id="Text2" type="text" onkeyup="submitOnEnter(event, 'p2.html')" />