jQuery .serializeObject 不是函数 - 仅在 Firefox 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8900587/
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
jQuery .serializeObject is not a function - only in Firefox
提问by Wasim
I'm using jQuery, and specifically this function
我正在使用 jQuery,特别是这个函数
$("#postStatus").serializeObject();
$("#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
它在 Chrome 和 Safari 中运行得非常好,但是当我在 Firefox 中运行时它不起作用。我用 Firebug 看看它给出了什么错误,我得到了这个
$("#postStatus").serializeObject is not a function
$("#postStatus").serializeObject is not a function
Why doesn't this function work in Firefox?
为什么这个功能在 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.
我设法通过将上面的函数放在 JS 文件的顶部来解决这个问题。谢谢你们的帮助。
采纳答案by Rafay
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
AFAIK jQueryserializeObject
在其核心中没有定义任何功能。可能您正在使用一个插件,并且它仅在 Firefox 中存在问题,因此可以安全地假设您的脚本包含顺序正确,请尝试将您的代码包装在就绪处理程序中
$(function(e){
$("#postStatus").serializeObject();
});
or you can place the javascript at the bottom of the page.
或者您可以将 javascript 放在页面底部。
回答by Neil Villareal
You might also want to check this out https://github.com/citnvillareal/serializeObject.
您可能还想查看https://github.com/citnvillareal/serializeObject。
Sample Usage
示例用法
<form>
<input type="text" name="txt01[][name]" value="Text 01" />
<input type="text" name="txt01[][phone]" value="000001" />
<input type="text" name="txt01[][name]" value="Text 02" />
<input type="text" name="txt01[][phone]" value="000002" />
<input type="submit" value="Submit" />
</form>
<script>
( function( $){
$( document ).ready( function(){
$( "form" ).submit( function( e ) {
e.preventDefault();
var jsonObject = $( this ).serializeObject();
console.log( jsonObject );
} );
} );
} )( jQuery );
</script>
Console Output
控制台输出
Object
{
txt01: Array(2)
{
0: Object
{
name: Text 01
phone: 000001
},
1: Object
{
name: Text 02
phone: 000002
}
}
}
For more details click here.
欲了解更多详情,请点击此处。
回答by Ba.Lal
Try serialize() or serializeArray() instead serializeObject()
尝试 serialize() 或 serializeArray() 代替 serializeObject()