jQuery-在已知元素之后找到下一个input:text元素 [英] Jquery - find the next input:text element after a known element
问题描述
我有一个<tr id="tagTR">
的ID
鉴于以上所述,有可能找到下一个input:text
元素,而不管两者之间是否有任何其他标记.在这种情况下是否可以使用jQuery选择器?
I have an id for a <tr id="tagTR">
Given the above, is it possible to find the next input:text
element regardless of any other mark up in between . Is there a jQuery selector that I can use for this scenario?
例如:
<tr id="tagTR">
</tr>
<tr id="tagRed">
<td> </td>
</tr>
<div>
<tr>
<td>
<input> // This is what I want to get to.
</td>
</tr>
</div>
推荐答案
我认为这个问题非常有趣.似乎其他人正在读这篇文章,在兄弟姐妹中寻找下一个输入.但我的读法是-无论如何都找到下一个输入.我不知道它是否在同胞,父母或父母的同胞中.这是我根据从
I thought this question was very interesting. It seems others are reading this as, find the next input among siblings. But I read it as - find me the next input no matter what. I don't know if its in a sibling, a parent or a parent's sibling. This is what I came up with based on feedback I received from this question.
//assume you know where you are starting from
var $startElement = $('#foo');
//get all text inputs
var $inputs = $('input[type=text]');
//search inputs for one that comes after starting element
for (var i = 0; i < $inputs.length; i++) {
if (isAfter($inputs[i], $startElement)) {
var nextInput = $inputs[i];
alert($(nextInput).val());
}
}
//is element before or after
function isAfter(elA, elB) {
return ($('*').index($(elA).last()) > $('*').index($(elB).first()));
}
这篇关于jQuery-在已知元素之后找到下一个input:text元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!