处理表中的jQuery单击事件

mko*_*mko 2 jquery textbox row click

我有一个表,其中包含几行,每行包含一个按钮和一个文本框.

使用文本框值触发警报的最简单方法是什么?

mea*_*gar 7

您可以将单击处理程序绑定到按钮,该按钮遍历DOM到其包含的内容<tr>,然后从那里找到文本框:

// Bind a click handler to all buttons in the table...
$('table :button').click(function () {

  var text = $(this)    // "this" is the button which was clicked
    .closest('tr')      // find the <tr> which contains it...
    .find(':text')      // find the text box within that <tr>...
    .val();             // and get its value

  alert(text);
});
Run Code Online (Sandbox Code Playgroud)