Why a button is clicked when a form is submitted?

前端 未结 2 1595
礼貌的吻别
礼貌的吻别 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 13:02

    It actually doesn't automatically press the button, that isn't what's submitting the form. The form is being submitted because that's a default characteristic of text boxes - if you have focus in the text box, pressing the enter key will cause an attempt to submit the form. This article goes into a deeper explanation: https://www.tjvantoll.com/2013/01/01/enter-should-submit-forms-stop-messing-with-that/

    EDIT: Actually, more specifically, a form will only submit on the enter key if: 1)A submit button exists in the form, or 2)The form only contains one text input

    If neither of these conditions is true, then the enter key should not submit the form

提交回复
热议问题