如何使用jQuery发布包含多个字段的表单? [英] How to post a form with many fields with jQuery?

查看:103
本文介绍了如何使用jQuery发布包含多个字段的表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管$ .ajax()可以用来做Ajax的事情,但我认为它不适合发布大型表单的值。



post 大型表单(很多字段),而无需手动输入全部信息?

你在这个假设背后的推理是什么? POST被设计用于传输比GET更多的数据。 AJAX POST请求与正常POST请求几乎完全一样,只是以一种稍微不同的方式捆绑并由浏览器在内部处理。几个标题可能会略有不同,但数据完全相同。为什么AJAX无法处理大形式?



无论如何,您甚至可以定义为大形式?

编辑:感谢您澄清您的问题。我明白你现在问的是什么,并且我知道你来自哪里。对于包含大量输入的表单,将它整合到Ajax请求中可能会很痛苦。

由于您使用的是jQuery,因此有一个轻松解决这个问题。查看序列化()方法。你给它一个表单,它会返回一个包含所有表单输入元素和值的查询字符串,这些元素和值可以直接传递给ajax请求。



您只需要这样做:



< pre $ $。ajax({
data:$(form)。serialize(),
// etc。
});

其中form是你的表单。


Though $.ajax() can be used to do ajax things, I don't think its fit for posting values of a big form.

How would you post a big form (many fields) without entering all them by hand?

解决方案

What is your reasoning behind that assumption? POST is designed for transferring larger amounts of data than GET. An AJAX POST request is almost exactly the same as a "normal" POST request, it's just bundled and handled internally by a browser in a slightly different manner. A couple of headers might be slightly different, but the data is all the same. Why should AJAX fail to handle a "large" form?

What would you even define as a "large" form anyway?

Edit: Thanks for the clarification on your question. I understand what you're asking now, and I see where you're coming from. For a form with a lot of inputs, it could be a pain to bundle it up into an Ajax request all the time.

Since you're using jQuery, there's an easy solution to this. Check out the serialize() method. You give it a form, and it gives you back a query string of all the form input elements and values which you can pass directly to an ajax request. There's an example there on the manual page that shows how it's done.

All you have to do is this:

$.ajax({
    data: $("form").serialize(),
    //etc.
});

where "form" is the id of your form.

这篇关于如何使用jQuery发布包含多个字段的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆