我有一个动态表,表中的每一行都有带有唯一 ID 的删除按钮,我正在做的是单击按钮发布到运行要删除的查询的 delete.jsp 页面。我需要的是 onlcik 传递单击发布的按钮的唯一 ID,以便查询可以匹配数据库中的 ID 并删除该行。很抱歉,如果我不够清楚,请查看下面的代码以获取更多解释。任何帮助将不胜感激。
function{
//dynamically generating table which (have rows and one of the columns have buttons to delete)
//each dynamically generated button has unique ID coming from DB
//button is created inside js function
//each table row have buttons with uinqe ID
for eg:
'<input type="button" id="each unique ID from db" class="dingdong">'
}
//now i am using onclick function to delete the row
$(".dingdong").click(function() {
$.post("testdelete.jsp", {
//i need to pass each Unique ID(ID of the button which was clicked) to this post
id
}, function(data) {
});
});
//in testdelet.jsp i have this query
//Delete *from testdb where ID=?
最佳答案
在事件监听器中,您可以使用 $(this)
获取单击的 jquery 元素:
$(".dingdong").click(function() {
var element = $(this);
var parent = element.parent();
// detach button from dom, so it can't be clicked twice
element.detach();
$.post("testdelete.jsp", {
id: element.attr('id');
}, function(data) {
// on success remove the dom element representing your object.
// I'm guessing you display the data in some sort of table
element.remove();
parent.closest('tr').remove();
});
});
关于javascript - 传递按钮 ID 进行发布。 js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44610749/