form表单内所有元素追加name属性值
<form class="form-horizontal m" id="feedback"><input id="type" name="type" type="text" value="test1"><input id="ruleId" name="ruleId" type="text" value="test2"><input id="usercode" name="usercode" type="text" value="test3"><input id="username" name="username" type="text" value="test4"><input id="bah" name="bah" type="text" value="test5"></form>
获取表单数据的方式一:
let json = $('#feedback').serialize();console.log('json : ',json); // 输出:type=test1&ruleId=test2
获取表单数据的方式二:
let data = {};var value = $('#feedback').serializeArray();$.each(value, function (index, item) {data[item.name] = item.value;});let json = JSON.stringify(data);console.log('json : ',json);/** 输出:{"type":"test1","ruleId":"test2"}*/