将这个(一个按钮 html 元素)传递给PP_6函数



我使用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();
}

相关内容

  • 没有找到相关文章

最新更新