How to serialize a form into an object with jQuery? -
$('form').serialize()
generate a=1&b=2
,but want {a:1,b:2}
.
how this?
further more,is possible kind of job arbitrary container div
?
jquery doesn't support this. you'll have use json.stringify, supported in modern browsers, support in older browsers, have include json2 library <script>
you have like:
(function ($) { jquery.fn.jsonserialize = function () { var obj = {}; var form = this[0]; if (form.tagname !== "form") { return ""; } $(form.elements).each(function () { obj[this.name] = $(this).val(); }); return json.stringify(obj); } }(jquery));
then use follows:
var stringified = $('#yourform').jsonserialize();
Comments
Post a Comment