开发者

Serialize form to a specific json format

开发者 https://www.devze.com 2023-03-18 09:45 出处:网络
When I use .serializeArray(), my data looks like: [ Object { Name=\"Name\", Value=\"MyName\" }, Object { N开发者_JS百科ame=\"Age\", Value=\"15\"} ]

When I use .serializeArray(), my data looks like:

[ Object { Name="Name", Value="MyName" }, Object { N开发者_JS百科ame="Age", Value="15"} ]

But I want it to look like:

{ Name: "MyName", Age: 15 }

What do I need to do?

My form:

<form id="newUser">
<input id="Name" name="Name" />
<input id="Age" name="Age" />
<input type="submit" />
</form>


You could write a bit of code which takes the first format and then converts it into the other. Just have it loop over the array and take Name as a key in the object, and Value as the value for the key and assign each pair into a single object.

0

精彩评论

暂无评论...
验证码 换一张
取 消