防止在按Enter键时提交表单

防止在按Enter键时提交表单

问题描述:

我们如何防止在按Enter键的同时提交表单.

how can we prevent a form from being submitted while pressing enter key.

实际上,我有一个文本框,在该文本框上输入一个值并单击Enter时,textbox2将变得集中.

Actually I have a text box and on entering a value on that textbox and clicking enter the textbox2 will get focused.

默认情况下,单击输入按钮将提交表单.因此我无法解决问题.我在提交按钮的onclick事件上写了return false,然后它起作用了. 因此,我想知道如何在按Enter键时阻止表单提交.

By default on clicking the enter button the form will get submitted. So I can not get the out put. I wrote return false on onclick event of the submit button then it works. So I would like to know how can we prevent a form submission while pressing enter.

在不查找实际代码的情况下,这是我要怎么做的基本原理:

Without looking up the actual code, here's the basic theory behind how I'd do it:

在文档上定义一个事件处理程序,以按下一个键来调用函数.

Define an event handler on the document for a key being pressed to call a function.

如果触发事件的键是回车键,则为return false,否则为return true.

If the key that triggered the event is the enter/return key, return false, otherwise return true.

看来您已经找到了答案(以及执行此操作的一些代码),所以现在我的答案有些不必要了.

Looks like you already found the answer (as well as some code to do it), so my answer is somewhat unnecessary now.