1function setJson1() {
var tbl = document.getElementById("tbl");
var jsonArr = new Array();
for(var i=0; i < tbl.rows.length; i++) {
var jsonobj = {};
//jsonobj.powCd = tbl.rows[i].cells[0].firstChild.value;
//jsonobj.powName = tbl.rows[i].cells[1].innerHTML;
//jsonobj.startDate= tbl.rows[i].cells[2].innerHTML;
//jsonobj.endDate = tbl.rows[i].cells[3].innerHTML;
jsonobj["powCd"] = tbl.rows[i].cells[0].firstChild.value;
jsonobj["powName"] = tbl.rows[i].cells[1].innerHTML;
jsonobj["startDate"]= tbl.rows[i].cells[2].innerHTML;
jsonobj["endDate"] = tbl.rows[i].cells[3].innerHTML;
//alert(tbl.rows[i].cells[0].firstChild.value);
//alert(tbl.rows[i].cells[1].innerHTML);
//alert(tbl.rows[i].cells[2].innerHTML);
//alert(tbl.rows[i].cells[3].innerHTML);
jsonArr.push(jsonobj);
}
alert(jsonArr.length);
for(var i=0; i < jsonArr.length; i++) {
alert(jsonArr[i]["powCd"]);
alert(jsonArr[i]["powName"]);
}
}
2function setJson2() {
var tbl = document.getElementById("tbl");
for(var i=0; i < tbl.rows.length; i++) {
var jsonobj = {};
jsonobj["powCd"] = tbl.rows[i].cells[0].firstChild.value;
jsonobj["powName"] = tbl.rows[i].cells[1].innerHTML;
jsonobj["startDate"]= tbl.rows[i].cells[2].innerHTML;
jsonobj["endDate"] = tbl.rows[i].cells[3].innerHTML;
var e = document.createElement("input");
e.type = "hidden";
e.name = "power";
e.value="jsonobj";
//document.getElementById("aa").appendChild(e); //ok
document.all.myform.appendChild(e);
}
}
<body onload = "loadSel()" id="aa">
<form name="myform">
<table id="tbl">
<tr id="tr1"><td><input type="checkbox" value="1"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
<tr id="tr2"><td><input type="checkbox" value="2"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
<tr id="tr3"><td><input type="checkbox" value="3"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
<tr id="tr4"><td><input type="checkbox" value="4"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
</table>
<input type="button" id="tb" value="setJson from table" οnclick="setJson()"/>
</form>
</body>
var tbl = document.getElementById("tbl");
var jsonArr = new Array();
for(var i=0; i < tbl.rows.length; i++) {
var jsonobj = {};
//jsonobj.powCd = tbl.rows[i].cells[0].firstChild.value;
//jsonobj.powName = tbl.rows[i].cells[1].innerHTML;
//jsonobj.startDate= tbl.rows[i].cells[2].innerHTML;
//jsonobj.endDate = tbl.rows[i].cells[3].innerHTML;
jsonobj["powCd"] = tbl.rows[i].cells[0].firstChild.value;
jsonobj["powName"] = tbl.rows[i].cells[1].innerHTML;
jsonobj["startDate"]= tbl.rows[i].cells[2].innerHTML;
jsonobj["endDate"] = tbl.rows[i].cells[3].innerHTML;
//alert(tbl.rows[i].cells[0].firstChild.value);
//alert(tbl.rows[i].cells[1].innerHTML);
//alert(tbl.rows[i].cells[2].innerHTML);
//alert(tbl.rows[i].cells[3].innerHTML);
jsonArr.push(jsonobj);
}
alert(jsonArr.length);
for(var i=0; i < jsonArr.length; i++) {
alert(jsonArr[i]["powCd"]);
alert(jsonArr[i]["powName"]);
}
}
2function setJson2() {
var tbl = document.getElementById("tbl");
for(var i=0; i < tbl.rows.length; i++) {
var jsonobj = {};
jsonobj["powCd"] = tbl.rows[i].cells[0].firstChild.value;
jsonobj["powName"] = tbl.rows[i].cells[1].innerHTML;
jsonobj["startDate"]= tbl.rows[i].cells[2].innerHTML;
jsonobj["endDate"] = tbl.rows[i].cells[3].innerHTML;
var e = document.createElement("input");
e.type = "hidden";
e.name = "power";
e.value="jsonobj";
//document.getElementById("aa").appendChild(e); //ok
document.all.myform.appendChild(e);
}
}
<body onload = "loadSel()" id="aa">
<form name="myform">
<table id="tbl">
<tr id="tr1"><td><input type="checkbox" value="1"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
<tr id="tr2"><td><input type="checkbox" value="2"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
<tr id="tr3"><td><input type="checkbox" value="3"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
<tr id="tr4"><td><input type="checkbox" value="4"/></td><td>a2</td><td>a3</td><td>a4</td></tr>
</table>
<input type="button" id="tb" value="setJson from table" οnclick="setJson()"/>
</form>
</body>