【发布时间】:2015-05-13 13:19:21
【问题描述】:
我目前有一个表单,可以使用 javascript 在点击时添加新条目。但是,我想更改每个后续“附加组件”的 id:例如第一个 tbody 的 id 为“participant1”,但下一个 tbody 的 id 为“participant2”,第八个为“participant8”,依此类推。
这是我的主文件的摘录:
<fieldset class="row2">
<legend>List of Participants</legend>
<p>
<input type="button" value="Add Participant" onClick="addRow('dataTable')" />
<input type="button" value="Clear Participants" onClick="deleteRow('dataTable')" />
<p>(All acions apply only to entries with check marked check boxes only.)</p>
</p>
<table id="dataTable" class="form" border="1">
<tbody>
<tr>
<p>
<td>
<input type="checkbox" required="required" name="chk[]" checked="checked" />
</td>
<td>
<div>Participant: </div>
<select name="participant1" id="participant1"> <option>Person A</option>
<option>Person B</option>
<option>Person C</option>
</select>
</td>
</p>
</tr>
</tbody>
</table>
<div class="clear"></div>
</fieldset>
这就是我目前正在尝试使用我的 JS 文件:
function addRow(tableID) {
var j=1;
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 50){
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.id="participant"+ j;
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
j++;
}
}else{
alert("More than 50 Participants? Are you sure?");
}
}
但这似乎只适用于一个条目,而不是所有条目。
【问题讨论】:
标签: javascript html forms function for-loop