最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing Parameters to jQuery Delete Button Click EventHandler - Stack Overflow

programmeradmin5浏览0评论

I have the following jQuery

$('#btnDelete').click( function() {//Do the delete here via jquery post});

In my table I have numerous rows with the following

<a id="btnDelete">Delete</a>

How I do I pass parameters to the click event to post an ID to delete something

I have the following jQuery

$('#btnDelete').click( function() {//Do the delete here via jquery post});

In my table I have numerous rows with the following

<a id="btnDelete">Delete</a>

How I do I pass parameters to the click event to post an ID to delete something

Share Improve this question asked May 15, 2009 at 20:59 JonJon 40.1k87 gold badges243 silver badges393 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

If you have many of these, the easiest way to tie a handler to them all is to have them all share a mon class rather than a mon ID (id's should be unique...).

<a class="btnDelete" id="del_123">Delete</a>
<a class="btnDelete" id="del_124">Delete</a>
<a class="btnDelete" id="del_125">Delete</a>

Then to activate them, you can bind a click event:

$(function() {
    $(".btnDelete").click(function() {
        // get your id here
        var $id = parseInt(this.id.substring(4));
        // POST with your info or whatever... and then...

        return false;
    });
});

You could use this wrapped in jQuery function. Inside your callback:

$(this)

will be a wrapped set containing the a tag that trigger the event. For example if, you have this structure:

<tr><td><a id="btnDelete">Delete</a></td></tr>

$(this).parent()

will contain the parent td element. You can then use its id or attributes. This is much better than trying to pass parameters to the callback.

发布评论

评论列表(0)

  1. 暂无评论