I make a table in a form to enable user to submit several item in one time. I am able to use jquery to get the first row of the value input by user. But when they type in the value in the second row, it show error. I am pretty sure there is something wrong with the array that I am using. I have try 2 methods, both method work but it only takes the value from first row only.
<form>
<table>
<thead>
<tr>
<th>Item Code</th>
<th>Item Name</th>
<th>Status</th>
</tr>
</thead>
<tbody id="entrybody">
<tr>
<td><input type="text" name="prd_code"></td>
<td><input type="text" name="prd_name"></td>
<td><input type="text" name="prd_cond"></td>
</tr>
<tr>
<td><input type="text" name="prd_code"></td>
<td><input type="text" name="prd_name"></td>
<td><input type="text" name="prd_cond"></td>
</tr>
<tr>
<td><input type="text" name="prd_code"></td>
<td><input type="text" name="prd_name"></td>
<td><input type="text" name="prd_cond"></td>
</tr>
</tbody>
</table>
</form>
$(function() {
$.post("action.php", function(data) {
$("[name='title']").val(data.title);
$("[name='body']").val(data.body);
}, "json");
setInterval(function() {
var itemlist = new Array;
var count = 1;
var title = $("[name='title']").val();
var body = $("[name='body']").val();
itemlist[count - 1] = {};
$("#productbody tbody tr").each(function() {
var that = $(this);
if (that.find("[name='prd_code']").val().length !== 0) {
/*itemlist.push(code);
itemlist.push(name);
itemlist.push(cond);*/ //method 1
itemlist[count - 1].code = that.find("[name='prd_code']").val();
itemlist[count - 1].name = that.find("[name='prd_name']").val();
itemlist[count - 1].cond = that.find("[name='prd_cond']").val(); //method 2
count++;
}
});
console.log(itemlist);
}, 2000);
});
===== Console message (first row) =====
0:
code: "test_code1"
cond: "test_status1"
name: "test_name1"
productbody