jQuery 拦截表单提交到参数字符串
我有一个表格。该表单通过 POST 提交到 iframe,iframe 依次处理请求,并根据结果执行 JavaScript,根据提交的有效性更改父级的内容。
现在,我不喜欢这个程序。我希望能够同时提交多个表单,但我只有一个隐藏的 iframe。所以我想用 AJAX 来实现,为每个表单提交创建一个单独的请求。
然而,我的表格很复杂;它由向数组添加变量的复选框、单击的图像及其需要发送的单击坐标以及类似的复杂内容组成 - 这就是为什么我不计算每个输入的值并将其添加到后参数字符串(顺便说一句:我不知道如何以这种方式创建数组),我更愿意截取提交内容,将其保存为包含所有这些参数的帖子字符串,然后将此字符串用于 AJAX发布请求。
我想在此功能中执行以下操作:
$('#myForm').submit(function(event){
// process the submission, e. g. event.getContent().toPostString();
// create the AJAX request and send it and attach listeners (I know how to do this line ;)
return false; // I don't want the form submitted (to the iframe)
});
提前致谢!
I have a form. This form submits via POST to an iframe, that, in turn, has the request processed and, depending on the result, a javascript is executed that changes the parent's content according the the submission's validity.
Now, I don't like this procedure. I want the ability to submit several forms simultaneously, but I have only this one hidden iframe. So I would like to do it with AJAX, creating a separate request for each form submission.
However, my form is complicated; it consists of checkboxes that add variables to arrays, of images that are clicked and whose click coordinates I need sent, and complicated stuff like that - which is why I, instead of calculating each the value of each input and adding it to a post parameter string (by the way: I don't know how I can create arrays that way), I would much prefer to rather have the submission content intercepted, saved as a post string with all those parameters, and then use this string for the AJAX POST request.
That I would like to do in this function:
$('#myForm').submit(function(event){
// process the submission, e. g. event.getContent().toPostString();
// create the AJAX request and send it and attach listeners (I know how to do this line ;)
return false; // I don't want the form submitted (to the iframe)
});
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用 iframe,只需使用 jQuery 的 ajax 方法:(我使用
post()
)这是一个示例:http://jsfiddle.net/maniator/Y6r8E/
在表单中输入一些内容并提交。
Don't use an iframe, just use jQuery's ajax methods: (I used
post()
in the example below)Here is an example: http://jsfiddle.net/maniator/Y6r8E/
Type something into the form and submit it.
jQuery 的 serialize() 函数将为您收集表单数据,以便通过 .ajax 轻松提交表单() 或 .post()。
jQuery's serialize() function will gather the form data for you, to allow for easy form submission via .ajax() or .post().