mardi 14 avril 2015

Assigning input values in a for-loop

I have a hash and would like to take each of those values and put them inside a text field. The one problem with what I have written out now is that the values are hardcoded in the HTML so if changes are to be made in a text field by the user they would be ignored when submitting a form. Is there any way I can insert the data from my hashes without them being explicitly written as a value attribute but still be processed on submit?


HTML



<form action="">
<table></table><br />
<input type="submit" />
</form>


JS



var data = [{name: "Object One", num: 1}, {name: "Object Two", num: 2}, {name: "Object Three",num: 3}];

for (var i = 0; i < data.length; i++) {
var dataNum = "<td><input type='text' value='" + data[i]["num"] + "'></td>",
dataName = "<td><input type='text' value='" + data[i]["name"] + "'></td>";
var $tr = "<tr>" + dataNum + dataName + "</tr>";

$("table").append($tr);
}

Aucun commentaire:

Enregistrer un commentaire