javascript 如何使用输入字段名称="array[]" jQuery ajax?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4787197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:53:27  来源:igfitidea点击:

How to jQuery ajax with input field name="array[]"?

javascriptjqueryajaxarraysserialization

提问by Vitaliy Isikov

I have several dynamically created hidden input fields. Most of which have a name formatted as array[]

我有几个动态创建的隐藏输入字段。其中大多数的名称格式为array[]

Question 1:

问题 1:

How can I use jQuery .ajax()or .post()to get the values from every field named array[]and pass them so they'll be retrievable as $_POST['array']in my PHP page?

我如何使用 jQuery.ajax().post()从每个命名的字段中获取值array[]并传递它们,以便它们可以像$_POST['array']在我的 PHP 页面中一样检索?

Question 2:

问题2:

Hypothetically speaking. Let's say that I don't know the name of said field but only the name of the form. How can I still do the same thing as in Question 1?

假设地说。假设我不知道所述字段的名称,而只知道表单的名称。我怎样才能仍然做与问题 1 相同的事情?

I found .serializeArray()in the jQuery documentation, but I have no idea what I'm doing with that and I'm not even certain if that applies to my situation of not knowing the field names.

.serializeArray()在 jQuery 文档中找到了,但我不知道我在做什么,我什至不确定这是否适用于我不知道字段名称的情况。

Thanks in advance.

提前致谢。

回答by Rocket Hazmat

You want to use .serialize()on the form. This will make a query string of all form elements (including 'name[]' ones).

您要.serialize()在表单上使用。这将生成所有表单元素(包括 'name[]' 元素)的查询字符串。

$.post('/url/to/post', $('#form').serialize(), function(data){
   alert('POSTed');
});

回答by Aaron Hathaway

You'll want to use jQuery's .serialize()method.
Check it out

你会想要使用 jQuery 的.serialize()方法。
看看这个