【发布时间】:2015-11-06 20:41:38
【问题描述】:
我想创建表并在用户单击按钮时动态添加行。对于同一个表列,我想获取数据 从数据库中并在下拉列表中显示它。我使用ajax函数来获取数据。 Ajax 功能也可以正常工作,但我无法添加 表格的新行。请帮我解决这个问题。
这是我的代码; 我觉得在 JavaScript 函数中添加 PHP 代码的方式有问题,但我不知道如何解决。请帮帮我。
html ....................................
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>No</th>
<th>Group</th>
<th>Quantity</th>
<th>Status</th>
<th>Cost</th>
<th><input type="button" class="addrow btn btn-success" value="Add Row "></th>
</tr>
</thead>
<tbody id="body">
<tr>
<th>1</th>
<td>
<?php $Treatments['#'] = 'Please Select'; ?>
<?php echo form_dropdown('tcode', $Treatments, '#', 'class="form-control" id="group"'); ?>
</td>
<td><input type="text" class="form-control quantity" size="3" id="quantity" name="quantity[]" size="3" /></td>
<td>
<select class="form-control status" id="status[]" name="status[]">
<option>Proposed</option>
<option>Done</option>
<option>Not Done</option>
<option>Not Required</option>
</select>
</td>
<td><input type="text" class="form-control cost" size="3" id="cost" name="cost[]" /></td>
</tr>
</tbody>
</table>
javascript ..................................................
$(function(){
$('.addrow').click(function(){
addrow();
});
function addrow()
{
var row =($('#body tr').length-0)+1;
var tr ='<tr>'+
'<th>'+row+'</th>'+
'<td>'+
'<?php $Treatments['#'] = 'Please Select'; ?>'+
'<?php echo form_dropdown('tcode', $Treatments, '#', 'class="form-control" id="group"'); ?>'+
'</td>'+
'<td><input type="text" class="form-control quantity" id="quantity" name="quantity[]" size="3"></td>'+
'<td><input type="text" class="form-control cost" size="3" id="cost" name="cost[]" /></td>'+
'</tr>';
$('#body').append(tr);
}
});
【问题讨论】:
标签: php jquery codeigniter dynamic html-table