【发布时间】:2016-05-26 11:31:53
【问题描述】:
我有一个包含表格的表格。表格每一行中的每个单元格都有一个输入字段或一个单选按钮。见下方代码:
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading"><b>Material [S]</b>upplied | <b>[R]</b>emoved | <b>[F]</b>ollow up
<button type="button" class="btn btn-success" id="addRow">Add Row <span class="glyphicon glyphicon-plus-sign"></span></button>
</div>
<!-- Table -->
<div class="table-responsive container-fluid" id="datatable_material">
<table class="table table-bordered table-hover" id="results_material">
<thead>
<tr class="tablehead_material">
<th>Qty</th>
<th>Part Number</th>
<th>Description</th>
<th>Serial Number</th>
<th>[S]</th>
<th>[R]</th>
<th>[F]</th>
</tr>
</thead>
<tbody id="material">
<tr>
<td>
<input type="text" class="form-control" id="qty" name="qty_1" placeholder="Quantity">
</td>
<td>
<input type="text" class="form-control" id="part_no" name="part_no_1" placeholder="Part No">
</td>
<td>
<input type="text" class="form-control" id="description" name="description_1" placeholder="Description">
</td>
<td>
<input type="text" class="form-control" id="serialnumber" name="serialnumber_1" placeholder="Serial Number">
</td>
<td>
<input type="radio" name="material_1" id="supplied_1" value="supplied">
</td>
<td>
<input type="radio" name="material_1" id="removed_1" value="removed">
</td>
<td>
<input type="radio" name="material_1" id="followup_1" value="followup">
</td>
</tr>
</tbody>
</table>
</div><!-- /.table-responsive -->
</div>
您可能会看到表格位于面板内,在面板的顶部,我有一个按钮,用于在必要时在表格中添加新行。
为了在表格中添加新行,我使用以下 JavaScript 代码:
$("#addRow").click(funtion(){
$('#results_material > tbody:last-child').append(
'<tr>
<td>
<input type="text" class="form-control" id="qty" name="qty_1" placeholder="Quantity">
</td>
<td>
<input type="text" class="form-control" id="part_no" name="part_no_1" placeholder="Part No">
</td>
<td>
<input type="text" class="form-control" id="description" name="description_1" placeholder="Description">
</td>
<td>
<input type="text" class="form-control" id="serialnumber" name="serialnumber_1" placeholder="Serial Number">
</td>
<td>
<input type="radio" name="material_1" id="supplied_1" value="supplied">
</td>
<td>
<input type="radio" name="material_1" id="removed_1" value="removed">
</td>
<td>
<input type="radio" name="material_1" id="followup_1" value="followup">
</td>
</tr>'
);
});
第一个问题:append 方法内的部分代码不能有空格,而是必须在一行中。这使得代码 难以阅读。有什么解决办法吗?
第二个问题:每行输入字段的名称应该不同(提交时需要)。例如第二行应该有 name="qty_2" 等。有什么想法吗?
【问题讨论】:
标签: javascript forms input