我使用jQuery:动态创建表的新行
addRowToTable() {
$('.table > tbody:last-child').append(
'<tr>' +
'<td name="name1" contenteditable="true"></td>' +
'<td name="name2" contenteditable="true"></td>' +
'<td name="name3" contenteditable="true"></td>' +
'<td name="name4" contenteditable="true"></td>' +
'<td id="save">' +
'<button>save</button>' +
'</td>' +
'<td id="delete-row">' +
'<button onclick="deleteRowFromTable($(this))">delete</button>' +
'</td>' +
'</tr>'
);
}
这是我的deleteRowFromTable(deleteRowButton)
:
deleteRowFromTable(deleteRowButton) {
$(deleteRowButton).closest('tr').remove();
}
,它与addRowToTable
方法在同一个类中。我想在onclick
属性中传递一个按钮。我同时尝试了onclick="deleteRowFromTable($(this))"
和onclick="deleteRowFromTable(this)"
,但都不起作用。有什么办法让它发挥作用吗?
分配id="删除行";传递给按钮,并将按钮id"this.id"传递给deleteRowFromTable函数,如下所示。
addRowToTable() {
$('.table > tbody:last-child').append(
'<tr>' +
'<td name="name1" contenteditable="true"></td>' +
'<td name="name2" contenteditable="true"></td>' +
'<td name="name3" contenteditable="true"></td>' +
'<td name="name4" contenteditable="true"></td>' +
'<td id="save">' +
'<button>save</button>' +
'</td>' +
'<td>' +
'<button id="delete-row" onclick="deleteRowFromTable(this.id)">delete</button>' +
'</td>' +
'</tr>'
);
}
deleteRowFromTable(id) {
$('#'+id).closest('tr').remove();
}