我正在尝试编写jquery语句
这是 HTML 代码:
<tr role="row" class="odd">
<td class="variant_title">2014 - CYZ51P/18</td>
<td class="">1</td>
<td>
<input name="price[114][1]" value="265700" class="form-control input_prices unmask" autocomplete="off" type="text">
<a data-year="2014" data-variant="CYZ51P/18" href="#" class="analyseItemChannel green">
</td>
</tr>
<tr role="row" class="even">
<td class="variant_title">2013 - NMR71E22/24</td>
<td class="">0</td>
<td>
<input name="price[111][1]" value="0" class="form-control input_prices unmask" autocomplete="off" type="text">
<a data-year="2013" data-variant="NMR71E22/24" href="#" class="analyseItemChannel green">
</td>
</tr>
所以,我正在寻找的是,当用户单击(a href)时,我需要获取该(a href)附近的输入中的数据。
这是我的 jquery,以及对我不起作用的内容:
$("a.analyseItemChannel").on('click', function (event)
{
event.preventDefault();
price = $('.input_prices', this).val();
console.log(price);
price = $(this).closest('.input_prices').val();
console.log(price);
});
有任何建议, 谢谢,
最佳答案
prev
:获取匹配元素集中每个元素的前一个同级元素。如果提供了选择器,则仅当它与该选择器匹配时才会检索前一个同级。
最接近的
:对于集合中的每个元素,通过测试元素本身并在 DOM 树中向上遍历其祖先来获取与选择器匹配的第一个元素。
$("a.analyseItemChannel").on('click', function (event)
{
event.preventDefault();
var price = $(this).prev('.input_prices').val();
console.log(price);
});
关于javascript - 获取 href 附近的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40406444/