admin管理员组文章数量:1391947
I am new to js and am trying to serialize the values in a form to get it ready for submission into ajax.
In my browser, I get this error and I am not sure why:
$('#myform').serializeObject()
Uncaught TypeError: undefined is not a function
Here is the code I am running:
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
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;
};
$(document).ready(function () {
console.log("Hello there! How are you?");
mydata = JSON.stringify($('#myform').serializeObject());
console.log(mydata);
});
Here is a jsfiddle I created to make it easier. I thought the problem could be in serializeObject
, but the debugger does not even get to that point.
UPDATE
Though I corrected this typo (which came up while I was creating the post), I get the same error. I tried this is firefox, and I got TypeError: $(...).serializeObject is not a function
. Strangely, it works fine on jsfiddle.
I am new to js and am trying to serialize the values in a form to get it ready for submission into ajax.
In my browser, I get this error and I am not sure why:
$('#myform').serializeObject()
Uncaught TypeError: undefined is not a function
Here is the code I am running:
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
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;
};
$(document).ready(function () {
console.log("Hello there! How are you?");
mydata = JSON.stringify($('#myform').serializeObject());
console.log(mydata);
});
Here is a jsfiddle I created to make it easier. I thought the problem could be in serializeObject
, but the debugger does not even get to that point.
UPDATE
Though I corrected this typo (which came up while I was creating the post), I get the same error. I tried this is firefox, and I got TypeError: $(...).serializeObject is not a function
. Strangely, it works fine on jsfiddle.
-
your code seems a bit off with the
JSON.stringify
call. – Daniel A. White Commented Jan 15, 2015 at 1:17
5 Answers
Reset to default 3You can use the $.serialize() function from JQuery.
$(document).ready(function () {
console.log("Hello there! How are you?");
mydata = JSON.stringify($('#myform').serialize());
console.log(mydata);
});
I would simply use jQuery's built in methods:
serialize: http://api.jquery./serialize/
or
param: http://api.jquery./jquery.param/
Or are you doing anything especially custom there?
You've had a typo or a mistake, you used JSON.stringify
directly on jQuery object, instead of on the result of .serializeObject()
Before:
JSON.stringify($('#myform')).serializeObject());
After:
mydata = JSON.stringify($('#myform').serializeObject());
Updated Demo: http://jsfiddle/gz3Lbkcj/1/
Well it is like re-inventing the wheel but try replacing following statement:
mydata = JSON.stringify($('#myform')).serializeObject());
with
mydata = JSON.stringify($('#myform').serializeObject());
As stringify
will return string representation of object and you will not be able to call serializeObject
on it.
My problem was that I had been calling my script before jquery was being loaded and therefore I was getting that error message. Duh. I am embarrassed, but wanted to post this just in case it can benefit someone.
本文标签: javascriptTypeError undefined is not a function when serializing a formStack Overflow
版权声明:本文标题:javascript - TypeError: undefined is not a function when serializing a form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744683030a2619529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论