Convert form data to JSON object
This question already has an answer here:
I added above form in JSFiddle and it displays JSON data as output.
Working JSFiddle
$(function() {
$('form').submit(function() {
$('#result').text(JSON.stringify($('form').serializeObject()));
return false;
});
});
Use this jQuery plugin .serializeJSON() to convert form data to JSON object.
<form id="my-profile">
<!-- simple attribute -->
<input type="text" name="fullName" value="Mario Izquierdo" />
<!-- nested attributes -->
<input type="text" name="address[city]" value="San Francisco" />
<input type="text" name="address[state][name]" value="California" />
<input type="text" name="address[state][abbr]" value="CA" />
</form>
Javascript:
$('#my-profile').serializeJSON();
// returns =>
{
fullName: "Mario Izquierdo",
address: {
city: "San Francisco",
state: {
name: "California",
abbr: "CA"
}
}
serializeJSON()
function returns a JSON object.
Working Jsbin example http://jsbin.com/oTimiGE/1/edit
try jquery serializeArray()
method
http://api.jquery.com/serializeArray/
$('form').submit(function() {
console.log($(this).serializeArray());
return false;
});
链接地址: http://www.djcxy.com/p/46330.html
上一篇: 在Jquery中将表单转换为关联数组
下一篇: 将表单数据转换为JSON对象