在不使用鼠标的情况下提交表单时,是否会调用提交按钮的onclick事件?

时间:2021-07-20 20:08:17

From my simple experiment, even when I submit a form using keyboard enter (without using mouse to click), the onclick event on submit button also got invoked, as if someone clicked it.

从我的简单实验中,即使我使用键盘输入提交表单(不使用鼠标点击),也会调用提交按钮上的onclick事件,就像有人点击它一样。

So is this always the case?

总是这样吗?

P.S. I'm using jsx in React 15.3.2

附:我在React 15.3.2中使用jsx

1 个解决方案

#1


1  

There are two ways click event could be triggered. If not considering any other event, binding the click event along with it (such as onhover = trigger click on an element - using JS or jQuery).

可以通过两种方式触发click事件。如果不考虑任何其他事件,请将click事件与其绑定(例如onhover = trigger点击元素 - 使用JS或jQuery)。

1. Form Submissions

In case of form, when you have type="submit" with Button or Input element. The form has it's type implicitly set to submit event, which submits your form.

在表单的情况下,当您使用Button或Input元素输入type =“submit”时。表单的类型隐式设置为提交事件,提交表单。

You can try that by not adding any type="submit" in button or input element.

您可以通过不在按钮或输入元素中添加任何type =“submit”来尝试。

2. Focus & Enter Key

When you have any element focused, and if user hits enter. It will fire the click event action on that too.

如果您有任何元素聚焦,并且用户点击进入。它也将触发click事件操作。

Like When any link or button element have the focus currently and you hit enter instead of clicking it. It will do the same, that is fire the click event for that particular element.

就像当任何链接或按钮元素当前具有焦点时,您点击输入而不是单击它。它将执行相同的操作,即触发该特定元素的click事件。

Thanks

谢谢

I really hope this helps you more! :)

我真的希望这会对你有所帮助! :)

#1


1  

There are two ways click event could be triggered. If not considering any other event, binding the click event along with it (such as onhover = trigger click on an element - using JS or jQuery).

可以通过两种方式触发click事件。如果不考虑任何其他事件,请将click事件与其绑定(例如onhover = trigger点击元素 - 使用JS或jQuery)。

1. Form Submissions

In case of form, when you have type="submit" with Button or Input element. The form has it's type implicitly set to submit event, which submits your form.

在表单的情况下,当您使用Button或Input元素输入type =“submit”时。表单的类型隐式设置为提交事件,提交表单。

You can try that by not adding any type="submit" in button or input element.

您可以通过不在按钮或输入元素中添加任何type =“submit”来尝试。

2. Focus & Enter Key

When you have any element focused, and if user hits enter. It will fire the click event action on that too.

如果您有任何元素聚焦,并且用户点击进入。它也将触发click事件操作。

Like When any link or button element have the focus currently and you hit enter instead of clicking it. It will do the same, that is fire the click event for that particular element.

就像当任何链接或按钮元素当前具有焦点时,您点击输入而不是单击它。它将执行相同的操作,即触发该特定元素的click事件。

Thanks

谢谢

I really hope this helps you more! :)

我真的希望这会对你有所帮助! :)