使用回车提交表单时,会触发按钮上的Click事件

使用回车提交表单时,会触发按钮上的Click事件

问题描述:

我有一个带有两个按钮的表单,一个输入[type = submit] 和一个普通的按钮这是取消按钮。

I have a form with two buttons, one input[type=submit] and one plain button which is the cancel button.

我有两个事件处理程序,一个绑定到上的表单提交和一个绑定到按钮 点击

I have two event handlers, one bound to the form on submit and one bound to the button on click.

当我按输入输入时提交表单时,按钮上的点击事件会触发(在我可能添加的提交事件之前),为什么会这样?

When I submit the form by pressing enter in an input the click event on the button fires (and before the submit event I might add), why is this?

在gecko和webkit中都会发生这种情况。

This happens in both gecko and webkit.

这是一个有效的例子:
http://jsfiddle.net/q3JPR/

Here's a working example: http://jsfiddle.net/q3JPR/

如果按Enter键提交我想要触发提交事件,而不是点击事件。

If you submit by pressing enter I want the submit event to trigger, not the click event.

如果您将按钮更改为< input type =button。 .. 然后你的事件会表现正常......这里是小提琴:

If you change your button to be <input type="button"... then your events will behave properly... here is the fiddle:

工作小提琴