当一个表单提交,为什么一个按钮被点击? [英] Why a button is clicked when a form is submitted?

查看:132
本文介绍了当一个表单提交,为什么一个按钮被点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我说,我有一个文本输入和一个提交按钮的表单。

如果有形式没有任何按键,只需提交事件触发,但如果与或没有类型的属性至少有一个按钮类型=提交,它点击它。

现在,当我在输入输入一些东西,然后preSS输入,我看到两个按钮,点击表单提交事件被触发。

示例:

\r
\r

<形式为GT;\r
  <输入类型=文本/>\r
  <按钮的onclick =警报('提交');>提交< /按钮>\r
< /形式为GT;

\r

\r
\r

我猜的形式自动单击窗体上的按钮提交事件。

我不知道的起源和对这种行为的原因是什么?
为什么我需要的按钮,当我提交表单被点击?


解决方案

从规范

4.10.22.2隐提交


  

如果用户代理支持让用户提交表单含蓄地(例如,在某些平台上打,而文本字段的重点是隐含提交表单回车键),那么这样做对于其默认的按钮有一个表单一个定义的激活行为必然导致用户代理上运行的默认按钮合成点击激活步骤。


<一个href=\"https://html.spec.whatwg.org/multipage/interaction.html#run-synthetic-click-activation-steps\">6.3 [点击合成]激活


  

在HTML中的某些元件具有一个活化行为,这意味着,用户可以激活它们。这触发依赖于激活机制,并且在点击事件通常最终的事件序列,如下所述。


  
  

用户代理应该允许用户手动触发使用键盘或语音输入端,有一个激活行为的元素,例如,或通过鼠标点击。当用户触发,在比单击它以外的方式定义的激活行为的元素,相互作用事件的默认操作必须是运行元素合成点击激活步骤。


<一个href=\"https://html.spec.whatwg.org/multipage/interaction.html#run-synthetic-click-activation-steps\">See这里完整剧本


所以pressing 输入被处理的话说提交在您的麦克风(使用语音输入设备) 。您的浏览器应该表现为,如果用户点击了按钮,正确处理click事件附加功能和提高可访问性对于那些谁不能使用鼠标。

Say, I have a form with a text input and a submit button.

If there are no buttons in the form, just submit event triggers, but if there is at least one button with no type attribute or with type="submit", it clicks it too.

Now, when I enter something in the input and then press Enter, I see that both button click and form submit events are triggered.

Example:

<form>
  <input type="text" />
  <button onclick="alert('submitted');">Submit</button>
</form>

I suppose the form clicks the button automatically on the form submit event.

I wonder the origins and the reason for such behavior? Why do I need the button to be clicked when I submit the form?

解决方案

From the spec:

4.10.22.2 Implicit submission

If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text field is focused implicitly submits the form), then doing so for a form whose default button has a defined activation behaviour must cause the user agent to run synthetic click activation steps on that default button.

6.3 [synthetic click] Activation

Certain elements in HTML have an activation behaviour, which means that the user can activate them. This triggers a sequence of events dependent on the activation mechanism, and normally culminating in a click event, as described below.

The user agent should allow the user to manually trigger elements that have an activation behaviour, for instance using keyboard or voice input, or through mouse clicks. When the user triggers an element with a defined activation behaviour in a manner other than clicking it, the default action of the interaction event must be to run synthetic click activation steps on the element.

See full script here


So pressing Enter is processed as saying submit in your microphone (with a voice input device). Your browser should behave as if the user has clicked the button to process the click event attached functions properly and improve accessibility for those who can't use a mouse.

这篇关于当一个表单提交,为什么一个按钮被点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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