按下Enter键时如何禁用自动提交行为? [英] How to disable auto submit behavior when hitting enter key?
本文介绍了按下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')" />
这篇关于按下Enter键时如何禁用自动提交行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文