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

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

    For me, the best solutions was this:

    $(form).submit(function(e){
    
       // Get the button that was clicked       
       var submit = $(this.id).context.activeElement;
    
       // You can get its name like this
       alert(submit.name)
    
       // You can get its attributes like this too
       alert($(submit).attr('class'))
    
    });
    

提交回复
热议问题