防止在按Enter键时提交表单 [英] prevent a form from being submitted while pressing enter key

查看:113
本文介绍了防止在按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.

这篇关于防止在按Enter键时提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆