admin管理员组

文章数量:1400783

Using either Javascript and/or Jquery how can I append POST data to a form to submit. I have in my server side code that will check to see if the post data dictionary contains a certain key.

I already have a form in the code. So I would just like to use javascript to add a new key to the POST data.

Using either Javascript and/or Jquery how can I append POST data to a form to submit. I have in my server side code that will check to see if the post data dictionary contains a certain key.

I already have a form in the code. So I would just like to use javascript to add a new key to the POST data.

Share Improve this question asked Aug 6, 2012 at 20:21 Christopher HChristopher H 2,1946 gold badges24 silver badges31 bronze badges 1
  • Both the answers from Jarry and Fabrizio will get you started in the right direction. – davidethell Commented Aug 6, 2012 at 20:31
Add a ment  | 

3 Answers 3

Reset to default 3

quick example

you can add hidden inputs, for example:

var yourValue = 123;
$('form').append('<input type="hidden" id="yourData" name="yourData" value="'+ yourValue +'"/>');

so you get:

<input type="hidden" id="yourData" name="yourData" value="123"/>

and when you do the submit you are sending "yourData"

with jQuery:

var $input = $('<input>').attr(
{
    type: 'hidden',
    id: 'input_id',
    name: 'input_name',
    value: 'input_value'
}).appendTo('#form_name');

$('#form_name').submit();

is this what you were trying to do?

try to intercept the submit event and:

  • serialize the form, add what you need to add and use the $.post(url, data, function(){}); function.
  • inject the form with extra hidden input fields and then submit it via jquery

本文标签: jqueryAppend POST data javascriptStack Overflow