如何使用jQuery在表结构中查找标记

时间:2011-08-03 22:21:09

标签: jquery-selectors

我有以下html:

<table>
  <tr>
    <td>
      <input type="button" id="btnAdd" value="Add" />
      <input type="hidden" id="hdnID" value='209' class="hdn" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" id="btnAdd" value="Add" />
      <input type="hidden" id="hdnID" value='210' class="hdn" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="button" id="btnAdd" value="Add" />
      <input type="hidden" id="hdnID" value='211' class="hdn" />
    </td>
  </tr>
</table>

我有一个添加按钮的jQuery点击事件。

$("#btnAdd").click(function ($e) {
  // Need selector here that can find the hidden field (see above) that is found 
  // in the same td as the button that was clicked. For example, if the Add button
  // in the 2nd row was clicked then I need to find the hidden input that has 
  // the value of '210'.

  // Doesn't work... not sure why
  var hdn = $(this).siblings().next();
});

提前感谢帮助我度过了一个艰难的时刻......

1 个答案:

答案 0 :(得分:1)

也许这样的事情可行吗?

("#btnAdd").click(function ($e) {
   var hdn = $(this).parent().next().children('td input');
});