How to add a post parameter to manual form submission?
I want to submit a form manually after some complicated checks. Because checks involve user interaction, the whole check process is not done synchronously. Here is the scenario:
- User clicks a button (an HTML
<button id='button-id'>
tag) - I prevent the default action of the button (which is the form submission)
- I do a complicated check.
- Somewhere in the check process, I show a dialog and wait for the user to respond (here, the original check function proceeds and returns)
- I provide a callback function for that dialog, wh开发者_Python百科ich fires and runs when the user closes it (either positive or negative result)
- Now, I should submit the form, but the original HTML
<button id='button-id'>
should be posted as a successful control. In other words, I should add thename
of the button tag as one of the posted parameters to the server.
I use jQuery's $('#form-id').submit()
method. How can I add the name
of the <button>
element to the HTTP Post parameters?
You could append a hidden field to your form before submitting -
$('<input>').attr({
type: 'hidden',
id: 'buttonid',
name: 'buttonid',
value: yourbuttonidvar
}).appendTo('#form-id');
Instead of using a button type, why not use
<input type="submit" name="commit" id="button_id" ...>
精彩评论