gpt4 book ai didi

javascript - 返回父元素jquery中的输入值

转载 作者:行者123 更新时间:2023-12-03 07:23:09 27 4
gpt4 key购买 nike

<tr role="row" class="odd parent">
<td class="quantity"><input type="text"></td>
</tr>

<tr class="child">
<td class="child" colspan="8">
<li data-dtr-offset-index-"7">
<span class="dtr-title"></span>
<span class="dtr-data">
<button type="button" class="btn btn-primary">Add to Cart</button>
</span>
</li>
</td>
</tr>

父行有一个数量输入。子行有一个按钮。我想点击按钮并返回数量。到目前为止我已经尝试过:

$(".btn-primary").click(function(e) {

var qty = $(this).closest("input").val();

});

但这返回未定义。

有人可以帮忙吗?

提前致谢!!

最佳答案

The parent row has an input for quantity. The child row has a button.

请注意,第一个 tr 不是第二个 tr 的父级; tr 元素是问题中 html 中的同级元素。

尝试将 .closest() 处的选择器调整为 tr ,将 .prev() 与选择器 tr 链接起来, .find() 和选择器 input , .val()

$(this).closest("tr").prev("tr").find("input").val()

关于javascript - 返回父元素jquery中的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116283/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com