获取元素相对于另一个父元素的值

时间:2014-01-13 11:16:02

标签: javascript jquery closest

[jQuery]如何获得相对于按钮的文本框值?

点击.button-action

会触发该事件
$(this).closest(".dmsInput").find('.d').val();

结构如下:

<td class="dmsInput">
 <input type="text" maxlength="4" size="4" class="d">
</td>
<td>
 <button class="button-action add">+</button>
</td>

我试图提醒值,但它返回undefined。我做得对吗?

1 个答案:

答案 0 :(得分:4)

这是因为.closest()将搜索祖先树,但dmsInput元素不是按钮元素的祖先。

$(this).parent().prev(".dmsInput").find('.d').val();

$(this).closest('tr').find('.dmsInput .d').val();