【发布时间】:2014-12-17 22:27:08
【问题描述】:
我正在向 jquery 的现有表中添加一个新行。新行由输入框组成,用于从用户输入输出。用户输入后,在按钮上单击我需要使这些文本框像前几行一样显示为表格的 td 元素?如何将文本框转换为表格的 td 元素。有人可以帮我解决这个问题吗?
HTML:
<table class="table table-hover" id="cust-table">
<thead>
<tr>
<th>LastName</th>
<th>FirstName</th>
</tr>
</thead>
<tbody>
<?php
for($i=0; $i<$numrows; ++$i) {
$contacts = mysqli_fetch_assoc($result);
?>
<tr>
<td><?php echo $contacts['LastName']; ?></td>
<td><?php echo $contacts['FirstName']; ?></td>
</tr>
<?php
} ?>
</tbody>
</table>
我的 jquery 代码:
function handleAdd (e) {
var row = $(this).parent().parent().parent().parent();
var form = '\
<tr>\
<td><input class="form-control"/></td>\
<td><input class="form-control"/></td>\
<td class="actions">\
<button class="btn btn-success btn-save-new"><i class="glyphicon glyphicon-ok"></i></button>\
<input type="hidden" id="SlNo" value=" ">\
</td>\
</tr>' ;
$('#cust-table tr:last').after(form);
$('.btn-save-new').click(function(){
var last_name = $($(this).parent().parent().children()[0]).children().val();
var first_name = $($(this).parent().parent().children()[0]).children().val();
//how to convert the textbox to table elements??
});
}
【问题讨论】:
-
您不想将此新条目添加到 $contacts 对象并使用 ajax 更新页面或重新加载吗?
标签: javascript jquery html