Why a button is clicked when a form is submitted?

前端 未结 2 1586
礼貌的吻别
礼貌的吻别 2021-02-20 12:27

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 ty

2条回答
  •  天命终不由人
    2021-02-20 12:56

    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.

提交回复
热议问题