-1

I'm having a Json problem when using serializeArray.

I have an HTML form like :

<form action="" method="post" name="myForm">
ID: <input type="text" name="id" /><br/>
State (XX): <input type="text" name="state" /><br/>
<p><input type="submit" onClick='submitform()' /></p>
</form>

With this in javascript:

function submitform() {
var formData = JSON.stringify($("form[name*='myForm']").serializeArray());
alert(formData);

The alert is outputting in name value pairs like:

[{"name":"id","value":"1234"},{"name":"state","value":"CA"}

What I really want is to output: {"id":"1234","state":"CA"}

Any suggestions?

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
mmm2893
  • 17
  • 6

1 Answers1

0

stackoverflow already has allot of posts on these

Serialize form data to JSON

   $.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;
    };
Community
  • 1
  • 1
Steve
  • 1,995
  • 2
  • 16
  • 25