How to add a post parameter to manual form submission?

自闭症网瘾萝莉.ら 提交于 2019-12-22 08:22:15

问题


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:

  1. User clicks a button (an HTML <button id='button-id'> tag)
  2. I prevent the default action of the button (which is the form submission)
  3. I do a complicated check.
  4. Somewhere in the check process, I show a dialog and wait for the user to respond (here, the original check function proceeds and returns)
  5. I provide a callback function for that dialog, which fires and runs when the user closes it (either positive or negative result)
  6. 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 the name 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?


回答1:


You could append a hidden field to your form before submitting -

$('<input>').attr({
    type: 'hidden',
    id: 'buttonid',
    name: 'buttonid',
    value: yourbuttonidvar  
}).appendTo('#form-id');



回答2:


Instead of using a button type, why not use

<input type="submit" name="commit" id="button_id" ...>


来源:https://stackoverflow.com/questions/7751650/how-to-add-a-post-parameter-to-manual-form-submission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!