在已创建好的table表格中动态的添加一行。
动态添加表格方法
利用after在表格中的一行中动态添加
jquery代码
function butt(th,val) {
var table= th.parentNode.parentNode
$.post("FindAll","gid="+val,function (data1) {
for (var i in data1) {
$(table).after("<tr>"+
"<td><img src='/ssm_GoodsSystem/upload/"+data1[i].picture+"' width='180px' height='120px'></td>"+
"<td>"+data1[i].description+"</td>"+
"</tr>");
}
},"json")
}
function stockA() {
var s=$("form").serialize();
$.post("FindAll?"+s,function (data) {
$("#tb").empty();
for (var i in data){
$("#tb").append("<tr id='tr'>\n" +
" <th>"+data[i].name+"</th>\n" +
" <th>"+data[i].typename+"</th>\n" +
" <th>"+data[i].brand+"</th>\n" +
" <th>"+data[i].model+"</th>\n" +
" <th>"+data[i].price+"</th>\n" +
" <th><input type='button' onclick=butt(this,"+data[i].gid+") value='详情'></th>\n" +
" </tr>")
}
},"json")
}
总结:使用此方法就可实现对表的添加,使用此方法的前提是要获得此行。根据获得这行找到父节点然后进行添加。
还有一种方法试试使用appendto来实现。这方法也是的找到父节点进行添加。