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.

Share Improve this question edited Jan 15, 2015 at 5:46 Trewq asked Jan 15, 2015 at 1:13 TrewqTrewq 3,2377 gold badges34 silver badges51 bronze badges 1
  • your code seems a bit off with the JSON.stringify call. – Daniel A. White Commented Jan 15, 2015 at 1:17
Add a ment  | 

5 Answers 5

Reset to default 3

You 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