serialize 和 serializeObject jquery 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17488660/
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
Difference between serialize and serializeObject jquery
提问by commit
I search a lot but didn't find perfect difference between serialize
and serializeObject
method of jquery.
我搜索了很多,但没有找到jqueryserialize
和serializeObject
method之间的完美区别。
Please help me to understand this.
请帮助我理解这一点。
回答by SpYk3HH
As you can see here, serializeObject
is not a native jQuery Method and thus only exist if you or a previous programmer of the site inserted it. As is mentioned in an Q&A found here, this function was probably found when someone working on your site "searched a way to serialize a form
" and found the following extension:
正如您在此处看到的,serializeObject
它不是本机 jQuery 方法,因此只有在您或该站点的前任程序员插入它时才存在。正如在此处找到的问答中提到的那样,当有人在您的网站“ searched a way to serialize a form
”上工作并发现以下扩展名时,可能会发现此功能:
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
Look for serializeObject
somewhere in your JS, but note, it's probably not needed as it appears to do the same thing as .$.fn.serialize
serializeObject
在您的 JS 中查找某处,但请注意,它可能不需要,因为它似乎与.$.fn.serialize
Upon further review, I found it's not the exact same. serializeObject
method found at other Q&A will serialize a form's value's as an Object, while serialize
encodes the values as a string for submission.
经过进一步,我发现它并不完全相同。serializeObject
在其他问答中找到的方法会将表单的值序列化为对象,同时serialize
将值编码为字符串以供提交。
Please take note, if you want somethinglike serailizeObject
that is native to the jQuery Core, then please see serializeArray
.
请注意,如果您想要serailizeObject
jQuery Core 原生的类似内容,请参阅serializeArray
.
The result will be slightly different in that serializeArray
will make an array of objects of your form values. each Object having { name: "", value: "" }
结果会略有不同,因为serializeArray
它将创建一个表单值的对象数组。每个对象有{ name: "", value: "" }
EXAMPLE
例子
Please seeDeveloper
Tools
Console
in example.请参见示例。Developer
Tools
Console
回答by Amit
$.serializeObject
is a variant of existing $.serialize
method which, instead of encoding form elements to string, converts form elements to a valid JSON
object which can be used in your JavaScript
application.
$.serializeObject
是现有$.serialize
方法的一种变体,它不是将表单元素编码为字符串,而是将表单元素转换为JSON
可在您的JavaScript
应用程序中使用的有效对象。
回答by Neil Villareal
I have done some digging here on stackoverflow on serializing form to json object and I ended up finding this method
我在stackoverflow上做了一些关于将表单序列化为json对象的挖掘,我最终找到了这个方法
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
yet it doesn't fit on what I was working on. So I created my own plugin. If your interested you might what to check this out https://github.com/citnvillareal/serializeObject
但它不适合我的工作。所以我创建了自己的插件。如果您有兴趣,您可以看看这个https://github.com/citnvillareal/serializeObject
回答by rktuxyn
I think this is easy way !TODO--
我认为这是简单的方法!TODO--
$.fn.serializeObject = function () {
var o = {};
this.find("[name]").each(function () {
o[this.name] = this.value;
});
return o;
};