jQuery 拦截表单提交到参数字符串

发布于 2024-11-15 18:12:53 字数 679 浏览 2 评论 0原文

我有一个表格。该表单通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守不住的情 2024-11-22 18:12:53

不要使用 iframe,只需使用 jQuery 的 ajax 方法:(我使用 post()

$('#myForm').submit(function(event){

             //url          //post data
    $.post(this.action, $(this).serialize(), function(returnData){
           //do something with returnData
    })

    return false; //do not submit form the normal way

});

这是一个示例:http://jsfiddle.net/maniator/Y6r8E/
在表单中输入一些内容并提交。

Don't use an iframe, just use jQuery's ajax methods: (I used post() in the example below)

$('#myForm').submit(function(event){

             //url          //post data
    $.post(this.action, $(this).serialize(), function(returnData){
           //do something with returnData
    })

    return false; //do not submit form the normal way

});

Here is an example: http://jsfiddle.net/maniator/Y6r8E/
Type something into the form and submit it.

何时共饮酒 2024-11-22 18:12:53

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文