jQuery .serializeObject不是一个函数 - 只在Firefox中 [英] jQuery .serializeObject is not a function - only in Firefox

查看:385
本文介绍了jQuery .serializeObject不是一个函数 - 只在Firefox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jQuery,特别是这个函数。
$ b $ p $(#postStatus)。serializeObject(); $ b

它在Chrome和Safari中绝对正常,但是当我在Firefox中使用它时,它不起作用。我用Firebug来看看它给了什么错误,我得到这个



$(#postStatus)。serializeObject不是为什么Firefox的这个函数不能工作?



更新。 ..

哦,是的,我完全忘了这不是核心功能。我记得我搜索了一种方式来序列化一个表单,并找到了这个解决方案;

  $。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文件的顶部,我设法解决了这个问题。感谢您的帮助。

解决方案

AFAIK jQuery没有函数定义为 serializeObject
< pre $ $(function(e){
$(#postStatus)。serializeObject();
});

或者您可以将javascript放在页面底部。

DEMO


I'm using jQuery, and specifically this function

$("#postStatus").serializeObject();

It works absolutely fine in Chrome and Safari, but when I do it in Firefox it doesn't work. I used Firebug to see what error it was giving, and i'm getting this

$("#postStatus").serializeObject is not a function

Why doesn't this function work in Firefox?

UPDATE...

Oh yes, I completely forgot that it's not a core function. I remember that I searched a way to serialize a form and found this solution;

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

I've managed to fix this issue by placing the function above at the top of the JS file. Thanks for your help guys.

解决方案

AFAIK jQuery has no function defined as serializeObject in its core. Probably you are using a plugin, and that its problematic in Firefox only so its safe to assume that your script inclusion is in proper order, try wrapping up your code in the ready handler

$(function(e){
$("#postStatus").serializeObject();
});

or you can place the javascript at the bottom of the page.

DEMO

这篇关于jQuery .serializeObject不是一个函数 - 只在Firefox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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