javascript - Covert form data to JSON string -
<form name = 'test' > <input type='text' name = 'login'> <input type='email' name = 'email'> </form>
if use json.serialize($(form)).serializearray();
[{"name":"login","value":"a value"},{"name":"email","value":"a email"}]
while need {"login":"a login","email":"a email"}
. how that??
you can pass <form>
formdata()
, iterate key, value pairs of formdata
instance, set each key , value object property , value
let form = document.forms["test"]; let fd = new formdata(form); let data = {}; (let [key, prop] of fd) { data[key] = prop; } data = json.stringify(data, null, 2); console.log(data);
<form name='test'> <input type='text' name='login' value="a login"> <input type='email' name='email' value="a email"> </form>
Comments
Post a Comment