Should I use the input's onclick or the form's onsubmit event to validate form fields using JavaScript?

前端 未结 2 1056
旧时难觅i
旧时难觅i 2021-01-18 09:53

I have a simple form with fields that I am trying to validate using JavaScript. In the form I have an input type of submit.

For validation, should I call the val

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 10:20

    onsubmit is triggered whenever the form is about to be submitted.
    onclick is triggered when the specific button is clicked.

    Forms can be submitted by hitting the enter key in any input field. This would not trigger the onclick of the submit button, but it would trigger the form submit event. As such, use onsubmit.

提交回复
热议问题