【发布时间】:2015-09-01 18:34:22
【问题描述】:
在下面的 js 代码中,我正在克隆表的最后一行并尝试将 ID 加一。
function addNewRow() {
$('#FinancialDataTable tbody').append($('#FinancialDataTable tbody tr:last').clone());
$('#FinancialDataTable tr').each(function(i) {
$(this).find('tr:last');
var selectinput = $(this).find('select');
var textinput = $(this).find('input');
var textarea = $(this).find('textarea');
i++;
selectinput.eq(0).attr('id', 'FinancialItem'+i);
textinput.eq(0).attr('id', 'FinancialAmount'+i);
textarea.eq(0).attr('id', 'FinancialDescription'+i);
});
}
我在表格中的表格行有以下代码:
<table id="FinancialDataTable">
<tr>
<td>
<div class="form-group">
<select class="form-control" id="FinancialItem"></select>
</div>
</td>
<td>
<div class="form-group">
<input class="form-control" id="FinancialAmount"></select>
</div>
</td>
<td>
<div class="form-group">
<textarea class="form-control" id="FinancialAmount"></select>
</div>
</td>
</tr>
</table>
<button type="button">Add New Row</button>
不是将新添加的行加一,而是将原始行更改为:
<table id="FinancialDataTable">
<tr>
<td>
<div class="form-group">
<select class="form-control" id="FinancialItem2"></select>
</div>
</td>
<td>
<div class="form-group">
<input class="form-control" id="FinancialAmount2"></select>
</div>
</td>
<td>
<div class="form-group">
<textarea class="form-control" id="FinancialAmount2"></select>
</div>
</td>
</tr>
</table>
<button type="button">Add New Row</button>
当我再次点击按钮时,我得到:
<table id="FinancialDataTable">
<tr>
<td>
<div class="form-group">
<select class="form-control" id="FinancialItem3"></select>
</div>
</td>
<td>
<div class="form-group">
<input class="form-control" id="FinancialAmount3"></select>
</div>
</td>
<td>
<div class="form-group">
<textarea class="form-control" id="FinancialAmount3"></select>
</div>
</td>
</tr>
</table>
<button type="button">Add New Row</button>
任何帮助将不胜感激! JSFIDDLE
【问题讨论】:
-
你为什么不关闭任何
div.form-groups? -
事故。它们在我的真实代码中是封闭的。现在应该修好了。