我想根据我输入的输入文本按回车键转到 p2.htm 或 p3.htm。我还想按 submit1 按钮手动发出 alert('no1')。
它在 FireFox 中有效,但在 IE6 中,当我按下回车键时,它会提交提交按钮。
如何在 IE 6 中像在 FireFox 中一样正确?
我使用 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>
最佳答案
如果使用 MVC3,您可以通过编辑 BeginForm 调用来禁用通过 Enter 提交,如下所示:
@using (Html.BeginForm("SomeAction", "SomeController", FormMethod.Post, new { onkeydown = "return event.keyCode!=13" }))
{
...
}
关于javascript - 按下回车键时如何禁用自动提交行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2020940/