开发者

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

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 validation function for onclick event 开发者_JAVA技巧on the input or onsubmit of the form? Is there any implication of choosing one over the other one?


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.


Both onclick and onsubmit do the same thing. No matter you press enter on a text input element or press submit button, both are triggered. If you use both onclick and onsubmit events, they are both executed respectively. And if You have more than one submit buttons in the form, the first submit button's onclick method is executed. Tested on Internet Explorer, Firefox, Chrome and Opera.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜