开发者

JQuery: Manipulating the Request Params in .submit()

开发者 https://www.devze.com 2023-01-22 22:27 出处:网络
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

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);
}


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消