yoursyun
form array to json ( 폼 배열을 json 객체로 변환 ) 본문
<form id="hello" />
<input type="text" name="cd_item" value="1111" />
<input type="text" name="qty" value="1" />
<input type="text" name="remark" value="g1" />
<input type="text" name="cd_item" value="1112" />
<input type="text" name="qty" value="2" />
<input type="text" name="remark" value="g2" />
<input type="text" name="cd_item" value="1113" />
<input type="text" name="qty" value="3" />
<input type="text" name="remark" value="g3" />
</form>
var formArray = $('#hello').serializeArray();
var formData = [];
for (var i = 0; i < formArray.length; i += 3) {
var cd_item = formArray[i].value;
var qty = formArray[i + 1].value;
var remark = formArray[i + 2].value;
if (qty > 0) {
formData.push({
cd_item: cd_item,
qty: qty,
remark: remark
});
}
}
var jsonData = JSON.stringify(formData);
console.log(jsonData);
const jsonObject = JSON.parse(jsonData);
jsonObject.forEach(function(item) {
console.log("cd_item:", item.cd_item);
console.log("qty:", item.qty);
console.log("remark:", item.remark);
});
반응형