传递按钮ID发布。 js jquery

时间:2017-06-18 00:43:02

标签: javascript jquery

我有一个动态表,表中的每一行都有一个带有UNIQUE ID的删除按钮,我正在做的是点击delete.jsp页面的按钮,该页面运行要删除的查询。我需要的是onlcik传递点击的UNIQUE ID按钮发布,这样查询可以匹配DB中的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=?

2 个答案:

答案 0 :(得分:1)

在事件监听器中,您可以使用$(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();
  });
});

答案 1 :(得分:0)

在您的点击事件处理程序中,可以通过this关键字访问点击的元素,并通过id属性

访问其ID
$.post("testdelete.jsp", {id: this.id}, function(data) {

});