JSFiddle - React, Tailwind, and code Playground
HTML
<h2>Form</h2>
<form action="" method="post">
Which city is in Great Britain?<br/>
<input type="hidden" name="first" value="1"/>
London:<input type="radio" name="first" value="11"/><br/>
New York:<input type="radio" name="first" value="12"/><br/>
Which city is in USA?<br/>
<input type="hidden" name="second" value="2"/>
Washington:<input type="radio" name="second" value="13"/><br/>
Tokio:<input type="radio" name="second" value="14"/><br/>
<p><input type="submit" /></p>
</form>
<h2>JSON</h2>
<pre id="result">
</pre>
CSS
form {
line-height: 2em;
}
p {
margin: 5px 0;
}
h2 {
margin: 10px 0;
font-size: 1.2em;
font-weight: bold
}
#result {
margin: 10px;
background: #eee;
padding: 10px;
height: 40px;
overflow: auto;
}
JavaScript
$.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;
}
$(function() {
$('form').submit(function() {
$('#result').text(JSON.stringify($('form').serializeObject()));
return false;
});
});