我有一个动态表,并且表中的每一行都有带有UNIQUE ID的删除按钮,我正在做的是单击按钮后的delete单击delete.jsp页面,该页面运行查询以进行删除。我需要的是onlcik传递单击单击以发布按钮的唯一ID,以便查询可以匹配数据库中的ID并删除该行。很抱歉,如果我不太清楚,请查看下面的代码以获取更多说明。任何帮助将不胜感激。javascript - 传递按钮ID进行发布。 js jQuery-LMLPHP



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

09-07 19:09
查看更多