Whats the difference between onclick and onsubmit?

后端 未结 4 942
执笔经年
执笔经年 2021-01-01 17:44

It\'s not like I haven\'t googled it... But still I couldn\'t understand when onsubmit is used and when onclick is used?

4条回答
  •  礼貌的吻别
    2021-01-01 18:05

    They're two completely separate events.

    onclick events fire when the user uses the mouse to click on something.

    onsubmit events fire when a form is submitted. The origin of this event can sometimes be traced back to an onclick (like clicking the "submit" button) but it can also come from a keyboard event (like pressing enter).

    This implies that using onclick on a submit button on a form might miss some cases that an onsubmit on the form would catch.

    There are many other kinds of events such as: onload for loading resources such as scripts or images and onkeydown for detecting key presses on the keyboard.

提交回复
热议问题