javascript - 传递按钮 ID 进行发布。 js

标签 javascript jquery

我有一个动态表,表中的每一行都有带有唯一 ID 的删除按钮,我正在做的是单击按钮发布到运行要删除的查询的 delete.jsp 页面。我需要的是 onlcik 传递单击发布的按钮的唯一 ID,以便查询可以匹配数据库中的 ID 并删除该行。很抱歉,如果我不够清楚,请查看下面的代码以获取更多解释。任何帮助将不胜感激。 enter image description here

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/

相关文章:

asp.net - 如何在 ASP.NET 中将对象列表转换为 JSON

javascript - 使用单选按钮隐藏/显示 div

javascript - 如何使原型(prototype)中的对象不在 javascript 中共享

jquery - Coda slider CSS

jQuery UI 选项卡 -- 多个动态创建的选项卡,选项卡内容全部显示在一个选项卡中

javascript - jQuery:在事件后包含外部脚本

jQuery 检查空字段不会检查所有字段

javascript - jQuery.kinetic - 如何在页面加载时预滚动到某个位置

javascript - 从 https 页面链接到 http 内容

c# - Javascript RegEx 不会工作,但在 c# 中工作(原子子表达式)