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

后端 未结 26 1629
旧巷少年郎
旧巷少年郎 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:40

    You want to use window.event.srcElement.id like this:

    function clickTheButton() {
    
    var Sender = window.event.srcElement;
    alert("the item clicked was " + Sender.id)
    
    }
    

    for a button that looks like:

    
    

    you will get an alert that reads: "the item clicked was myButton.

    In your improved example you can add window.event.srcElement to process_form_submission and you will have a reference to whichever element invoked the process.

提交回复
热议问题