开发者

Invoke click handler on class selector and retrieve the ID

开发者 https://www.devze.com 2023-02-09 18:15 出处:网络
I have an HT开发者_如何转开发ML Table with each row: <table> <tr><td><a href=\'#\' id=\'1\' class=\'delete\'>delete</a></td></tr>

I have an HT开发者_如何转开发ML Table with each row:

<table>
<tr><td><a href='#' id='1' class='delete'>delete</a></td></tr>
<tr><td><a href='#' id='2' class='delete'>delete</a></td></tr>
<tr><td><a href='#' id='3' class='delete'>delete</a></td></tr>
</table>

I need a jQuery function that gets the id when the click handler is called:

$('.delete').click(function() {
  //Get id and set to num
  var num = XXXXX //ID
  //Invoke ajax request using num
  jQuery.ajax({type:'POST',data:{'num': num}, url:'/user/delete',success:function(data,textStatus){deletePicture(num);;},error:function(XMLHttpRequest,textStatus,errorThrown){}});
});

Two questions: 1) How do I get the ID 2) Is the ajax success callback correct?


Your success callback looks fine (provided that deletePicture is defined elsewhere).

To get the id, you just need this.id:

var num = this.id;


var num = $(this).attr('id') I believe?

0

精彩评论

暂无评论...
验证码 换一张
取 消