views:

29

answers:

1

Is there a way to can access and manipulate the POST params after a form submit has been made. To be more specific, in my submit event handler, how can I do something like

$("#form").submit()
{
   //access the request params and manipulate them here
   return true;
}   

instead of the following hack which I find to be not that elegant.

$("#submit_button").click()
{
   // Construct the Request 'params' manually
   $.post("/foo/action", params, redirectIfSuccess);
}
+1  A: 

You can do it before form submit. Just check the variables in the form on submit event change one or more of them and submit with new data.

$("#form").submit(function() {
   $("#form #myInputField").val("Some text here");
   return true;
});

With ("#myInputField") you select the input field or you can use ("input[name='myInputField']") to select a input on its name attribute. After that you can use val() to get or val("Some text") to set the value of the appropriate input. That's all.

To add new input or DOM element you have to use .append(), Ex:

$("#form").append("<input type="hidden" name="myNewInput" value="1" />");

Have a look here: http://api.jquery.com/append/

infinity
infinity, How do I that? This is what I have in mind but I do not know how to access the params within the `$('#form').submit()` block. Is there some attribute I can access and add/delete/change stuff in place?
Sup3rkiddo
Thanks for the edit. I am guessing I can add new fields this way.
Sup3rkiddo