1

I have multiple buttons which can call the same JQuery function. Example of html:

<tr><td>...</td>
    <td><button class="modify" name="8">Modify</button></td>
</tr>
<tr><td>...</td>
    <td><button class="modify" name="9">Modify</button></td>
</tr>

and so on...

And my JQuery function:

$(function() {
    var id = $("button").attr("name");
    $("#dialog").dialog({
        autoOpen: false,
        height: 250,
        width: 240,
        modal: true,
        buttons: { 
            "Submit": function() {
                $( this ).dialog( "close" );
                $.ajax({
                    url: 'my_http',
                    type: 'POST',
                data: $my_data,
                }); 
            },
            Cancel: function () {
                $(this).dialog("close");
            }   
        }
    });
    $(".modify").click(function () {
        $("#dialog").dialog("open");
    }
});

As you can see I need to now which button was clicked (I retrieve it's name at the beginning of the dialog function). But since its "clickability" is determined by the class, not id, I get the first id in the list (8 in this case, even though the 9th was clicked).

How can I know which one was clicked? If I use classes, I do not know ids (names), if I use ids, how can I know that it was clicked?

4

2 に答える 2

1

次のように、クリック関数の最初のパラメーターを使用できます。

ライブデモはこちら!

$(".modify").click(function (e) {
  console.log(e.target);
});
于 2013-09-02T20:52:37.487 に答える