jQuery: how to get which button was clicked upon form submission?

后端 未结 26 1368
旧巷少年郎
旧巷少年郎 2020-11-22 16:01

I have a .submit() event set up for form submission. I also have multiple forms on the page, but just one here for this example. I\'d like to know which submi

26条回答
  •  花落未央
    2020-11-22 16:49

    When the form is submitted:

    • document.activeElement will give you the submit button that was clicked.

    • document.activeElement.getAttribute('value') will give you that button's value.

    Note that if the form is submitted by hitting the Enter key, then document.activeElement will be whichever form input that was focused at the time. If this wasn't a submit button then in this case it may be that there is no "button that was clicked."

提交回复
热议问题