Javascript 获取href附近的输入值
我正在尝试编写jquery语句 以下是HTML代码:Javascript 获取href附近的输入值,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试编写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
<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>
任何建议,
谢谢,在这种情况下,您应该使用而不是
prev
:获取匹配元素集中每个元素的前一个兄弟元素。如果提供了选择器,则仅当它与该选择器匹配时,才会检索上一个同级
nexture
:对于集合中的每个元素,通过测试元素本身并遍历DOM树中的祖先,获得与选择器匹配的第一个元素
您需要在jQuery中使用
prev()
函数来访问上一个同级:
$(“a.analyseItemChannel”)。在('click',函数(事件){
event.preventDefault();
price=$(this.prev('.input_prices').val();
控制台日志(价格);
price=$(this.prev('.input_prices').val();
控制台日志(价格);
});代码>
2014-CYZ51P/18
1.
2013年-NMR71E22/24
0
检查这是否是您想要的(首先导航到父级)
$(“a.analyseItemChannel”)。在('click',函数(事件)
{
event.preventDefault();
价格=$(this.prev().val();
控制台日志(价格);
//price=$(this).closest('.input_prices').val();
//控制台日志(价格);
});代码>
2014-CYZ51P/18
1.
2013年-NMR71E22/24
0
有很多方法可以做到这一点
$("a.analyseItemChannel").on('click', function (event) {
event.preventDefault();
//using siblings()
var price = $(this).siblings('.input_prices').first().val();
console.log(price);
//using prev()
price = $(this).prev('.input_prices').val();
console.log(price);
});
试试这个
$(document).on('click','a.analyseItemChannel', function(event){
event.preventDefault();
var nextInput = $(this).next().filter('input');
var prevInput = $(this).prev().filter('input');
if(nextInput.length > 0){
var price = nextInput.val();
console.log(price);
}
if(prevInput.length > 0){
var price = prevInput.val();
console.log(price);
}
});
为什么还要添加下一个?抱歉,实际上没有注意,现在修复了一些锚定标记,并按照其他人的建议切换到更合适的prev()
$("a.analyseItemChannel").on('click', function (event) {
event.preventDefault();
//using siblings()
var price = $(this).siblings('.input_prices').first().val();
console.log(price);
//using prev()
price = $(this).prev('.input_prices').val();
console.log(price);
});
$(document).on('click','a.analyseItemChannel', function(event){
event.preventDefault();
var nextInput = $(this).next().filter('input');
var prevInput = $(this).prev().filter('input');
if(nextInput.length > 0){
var price = nextInput.val();
console.log(price);
}
if(prevInput.length > 0){
var price = prevInput.val();
console.log(price);
}
});