[英]How to serialize form data into an object with nested properties
Given the following inputs: 给出以下输入:
<input name="person[1]['first']" />
<input name="person[2]['first']" />
<input name="person[3]['first']" />
I want to serialize this into an object like so: 我想将其序列化为这样的对象:
person = {
1: {first:value},
2: {first:value},
3: {first:value}
}
Is this functionality available in jQuery or javascript now? 现在可以在jQuery或javascript中使用此功能吗? or will I have to write a function to do it?
还是我必须编写一个函数来做到这一点?
当它位于<form>
标记内时,可以使用:
$(formElement).serialize();
You're looking for serializeArray()
您正在寻找
serializeArray()
EDIT 编辑
Adding short example on a form submit: 在表单提交中添加简短示例:
$('#container').on('submit', '#myForm', function(e) {
e.preventDefault();
var data = $(this).serializeArray(); // $(this) contains the form element
console.log(data); // will output serialized data
console.log(data.email); // will output email input value (if any)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.