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

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

    If what you mean by not adding a .click event is that you don't want to have separate handlers for those events, you could handle all clicks (submits) in one function:

    $(document).ready(function(){
      $('input[type="submit"]').click( function(event){ process_form_submission(event); } );
    });
    
    function process_form_submission( event ) {
      event.preventDefault();
      //var target = $(event.target);
      var input = $(event.currentTarget);
      var which_button = event.currentTarget.value;
      var data = input.parents("form")[0].data.value;
    //  var which_button = '?';       // <-- this is what I want to know
      alert( 'data: ' + data + ', button: ' + which_button );
    }
    

提交回复
热议问题