简体   繁体   中英

which submit button was clicked

How can I know that which submit button was clicked on submit the 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 am getting the form ID that is: create_tran_form . I want to get submit button's name or value. How can I achieve this? Thanks.

There is no way to tell from the submit event. You need to capture the event on the button.

eg have an event handler that listens for submit button clicks, store the result on the form, then read it back in on your submit handler.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM