在表中我有两个选择,都从数据库中获取值。 当按下添加按钮添加新行时,我想要将选择ID的名称更改为 facility2, currency2 facility3, currency3
等等。但我不知道如何改变它们。如何在添加表格行后更改选择标识的名称?
这是我所创建的表:
<table id="tableInformation">
<tr>
<td><label>No</label></td>
<td><label>Facility</label></td>
<td><label>Currency</label></td>
<td></td>
</tr>
<tbody>
<tr>
<td><label>1</label></td>
<td><div id="facility">
<select id="facility1" name="facility">
<option value=""></option>
<option value="AL">AL</option>
<option value="BL">BL</option>
<option value="CL">CL</option>
</select>
</td>
<td><div id="currency">
<select id="currency1" name="currency">
<option value=""></option>
<option value="idr">IDR</option>
<option value="usd">USD</option>
<option value="aud">AUD</option>
<option value="jpy">JPY</option>
</select>
</td>
<td><input type="button" id="btnAddRows" value=" + "
onclick=\'addRows()\' />
</tr>
</tbody>
</table>
这是JavaScript:
function addRows() {
var table = document.getElementById('tableInformation');
var rowCount = table.rows.length;
var iteration = rowCount -1;
var numbers = iteration +1;
var row = table.insertRow(rowCount);
var colCount = table.rows[0].cells.length;
// Cell for number
var cell = row.insertCell(0);
var label = document.createElement('label');
label.textContent = numbers;
cell.appendChild(label);
for(var i=1; i<colCount; i++) {
var newcell = row.insertCell(i);
newcell.innerHTML = table.rows[1].cells[i].innerHTML;
//alert(table.rows[1].cells[i].innerHTML);
//alert(newcell.childNodes);
}
}
在此先感谢。
哦,谢谢。我从来没有想过使用替换会是这个工作。我知道我必须小心使用替换,因为如果我拥有#1而不是id,它会改变值。还有一个问题,它是使用替换删除行吗? – candies
。@ bloodyknuckles – candies
你想知道如何删除一行吗?如果是这样,请阅读以下内容:[从表中添加/删除行](http://stackoverflow.com/a/13354190/2743458)。如果我误解了,请详细说明,也许问一个新的问题,如果它不是同一个主题。 – bloodyKnuckles