有没有一种方法可以在提交表单后访问和操作 POST
参数。更具体地说,在我的提交事件处理程序中,我该怎么做
$("#form").submit()
{
//access the request params and manipulate them here
return true;
}
而不是我发现不那么优雅的以下 hack。
$("#submit_button").click()
{
// Construct the Request 'params' manually
$.post("/foo/action", params, redirectIfSuccess);
}
最佳答案
您可以在表单提交之前完成。只需在提交事件更改其中一个或多个变量并提交新数据时检查表单中的变量。
$("#form").submit(function() {
$("#form #myInputField").val("Some text here");
return true;
});
您可以使用 ("#myInputField") 选择输入字段,也可以使用 ("input[name='myInputField']") 在其名称属性上选择输入。 之后,您可以使用 val() 获取或使用 val("Some text") 设置适当输入的值。就这样。
要添加新的输入或 DOM 元素,您必须使用 .append(), 例如:
$("#form").append("<input type="hidden" name="myNewInput" value="1" />");
关于javascript - JQuery:在 .submit() 中操作请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4058169/