var table = document.getElementById("table-body");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
cell1.innerHTML = ingredient_name;
cell2.innerHTML = ingredient_amount;
cell3.innerHTML = ingredient_unit;
cell4.innerHTML = '<button type="button" class="close" aria-.hidden="true"onClick="$(this).closest(\'tr\').remove()">×</button>';
如何设置我刚刚创建的行或表的ID?
答案 0 :(得分:7)
您只需设置新dom元素引用的id
属性
row.id='newid';
cell1.id='cellid1'
....
答案 1 :(得分:2)
在底部表格中添加行并设置动态ID。好又整洁的方式:
var newTr = $("<tr id="+e+"></tr>");
$("#TableID").append(NewTr);
我的例子:
function showimage(e) {
debugger;
for (var i = 0; i < e.length; i++) {
var fd = new FormData();
fd.append("ID", e[i].ID);
$.ajax({
url: "/AdminPanel/Pictures/ShowImage2",
type: 'POST',
contentType: false,
processData: false,
data: fd
}).then(function (e) {
var newTr = $("<tr id="+e+"></tr>");
var ImageTd = $('<td></td>');
var DeleteLinkTd = $('<td></td>');
var img = document.createElement("IMG");
img.src = "/AdminPanel/Pictures/ShowImage/"+e;
img.width = "100";
img.alt = "#" + e;
ImageTd.append(img);
var aTag = document.createElement('a');
aTag.setAttribute('onClick', "Delete("+e+");");
aTag.innerHTML = "Delete";
DeleteLinkTd.append(aTag);
newTr.append(ImageTd);
newTr.append(DeleteLinkTd);
$('#PicturesTable').append(newTr);
});}
$('#DivBulkUpload').dialog('close');
}