简体   繁体   中英

Get submit button value onsubmit event

I have a strange edge-case, let me try to describe it:

I have a form with multiple submit buttons with different values. The submit value is important in my backend. I want to intercept a form submit (using onsubmit ) do an asynchronous task and continue the event.

Sadly calling Form.prototype.submit() does not work, because the information which button was clicked is lost. Of course I can emulate that data, but just adding a hidden input, but I don't know how to figure out which button was clicked in the onsubmit event.

If you need an example this is where I am trying to solve it: https://github.com/codingjoe/django-s3file/blob/master/s3file/static/s3file/js/s3file.js

When you dynamically submit the form, you can do it differently than calling form.submit . You can just trigger the click event of whichever submit button you need to. Then, in a click event handler for the submit buttons, you can assign a value to your hidden form field with the appropriate data.

Also, know that if you just give your submit buttons a name attribute with a unique value and they will deliver their value as part of the form's data that gets submitted.

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