我有一个像这样的HTML表格:
<table id="dataTableLapis">
<tbody>
<tr id="prova1">
<td>
<select id="uff_prot" name="lapis_ufficio[]"></select>
</td>
<td>
<input "type="text" id="lapis_vis" name="lapis_vis[]" size="30">
</td>
<td>
<select id="lapis_ufficio" name="lapis_gruppo_mov[]"></select>
</td>
</tr>
</tbody>
</table>
这个HTML按钮:
<input type="button" value="Aggiungi ufficio protocollo" onClick="addRow('dataTableLapis')" />
运行此javascript:
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 5){
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.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Maximum Passenger per ticket is 5.");
}
}
我的问题是:当插入新行时,id总是一些
row1 - id =&#34; uff_prot&#34; - id =&#34; lapis_vis&#34; - id =&#34; lapis_ufficio&#34;
row2 - id =&#34; uff_prot&#34; - id =&#34; lapis_vis&#34; - id =&#34; lapis_ufficio&#34;
我想要这个结果:
row1 - id =&#34; uff_prot1&#34; - id =&#34; lapis_vis1&#34; - id =&#34; lapis_ufficio1&#34;
row2 - id =&#34; uff_prot2&#34; - id =&#34; lapis_vis2&#34; - id =&#34; lapis_ufficio2&#34;
我该怎么做?
答案 0 :(得分:0)
请在行变量声明下添加row.setAttribute行。
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
if(rowCount < 5){
var row = table.insertRow(rowCount);
row.setAttribute('id',"lapis_ufficio"+rowCount);
var colCount = table.rows[0].cells.length;
for(var i=0; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[0].cells[i].innerHTML;
}
}else{
alert("Maximum Passenger per ticket is 5.");
}
答案 1 :(得分:0)
不幸的是,这段代码不起作用:
var els = newRow.querySelectorAll("[id]");
for (var i = 0; i < els.length; i++) {
els.id += rowCount;
select id和input id是some,只更改tr id