The submit button was clicked on

How do I know which submit button is clicked to submit a form.

HTML:

<button type="submit" name="add_in_queue" id="add_in_queue" value="add_in_queue">Queue</button>
<button type="submit" name="create_tran" id="create_tran" value="create_tran">Process</button>

      

Jquery:

$('form#create_tran_form').on('submit',function(e){
    var submit_value = $(this).attr('id');
    alert(submit_value);
    e.preventDefault();
});

      

I get the forms ID: create_tran_form

. I want to get the name or value of a submit button. How can I achieve this? Thank.

+3


source to share


1 answer


Unable to tell about a submit event. You need to capture the event on the button.

eg. have an event handler that listens for the submit button clicks, stores the result in the form, and then reads it back in the submit handler.



$('[type="submit"]').on('click', function (evt) {
    $(this.form).data('selected', this.value);
});

$('form').on('submit', function (evt) {
    alert($(this).data('selected'));
});

      

+4


source







All Articles