[jQuery] 如何获取文本框相对于按钮的值?
点击.button-action时触发该事件
$(this).closest(".dmsInput").find('.d').val();
The structure goes like this:
<td class="dmsInput">
<input type="text" maxlength="4" size="4" class="d">
</td>
<td>
<button class="button-action add">+</button>
</td>
我尝试警告该值,但它返回未定义。我这样做对吗?
最佳答案
这是因为 .closest() 会搜索祖先树,但 dmsInput
元素不是按钮元素的祖先。
$(this).parent().prev(".dmsInput").find('.d').val();
或
$(this).closest('tr').find('.dmsInput .d').val();
关于javascript - 获取相对于另一个父元素的元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21089910/