serialize和serializeObject jQuery之间的区别 [英] Difference between serialize and serializeObject jquery

查看:161
本文介绍了serialize和serializeObject jQuery之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,但是没有找到jquery的serializeserializeObject方法之间的完美区别.

I search a lot but didn't find perfect difference between serialize and serializeObject method of jquery.

请帮助我理解这一点.

推荐答案

如您所见,此处serializeObject不是本机jQuery方法,因此仅当您或站点的以前的程序员插入它时才存在.正如在此处找到问题与解答所述,可能是有人在您的网站"searched a way to serialize a form"上工作并找到以下扩展名时发现的:

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:

$.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;
};

在您的JS中的某个位置查找serializeObject,但是请注意,它可能不需要,因为它看起来与$.fn.serialize 一样.

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方法会将表格的值序列化为对象,而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.

请注意, 如果您想要 之类的东西,例如serailizeObject jQuery Core的 ,请参阅 serializeArray .

Please take note, if you want something like serailizeObject that is native to the jQuery Core, then please see serializeArray.

结果将略有不同,因为serializeArray将形成具有表单值的对象数组.每个具有{ name: "", value: "" }

The result will be slightly different in that serializeArray will make an array of objects of your form values. each Object having { name: "", value: "" }

示例

请参阅 Developer Console .

EXAMPLE

Please see Developer Tools Console in example.

这篇关于serialize和serializeObject jQuery之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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