Javascript 使用 Jquery 查找下一个 textarea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4600165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find the next textarea using Jquery
提问by sarsnake
HTML
HTML
<tr id="rowId"><td><textarea class="inputTextarea"></textarea></td><td><textarea class="inputTextarea"></textarea></td></tr>
<tr id="rowId2"><td><textarea class="inputTextarea"></textarea></td><td><textarea class="inputTextarea"></textarea></td></tr>
<tr id="rowId3"><td><textarea class="inputTextarea"></textarea></td><td><textarea class="inputTextarea"></textarea></td></tr>
Provided I know rowId, how do I find the next textarea ON THIS PAGE, starting at any arbitary point. I don't mean ANY input, textarea only. I need to be able to start AT ANY row, and progress on to the next textarea, essentially going down the rows.
如果我知道 rowId,我如何在这个页面上找到下一个 textarea,从任意点开始。我的意思不是任何输入,仅 textarea。我需要能够从任何一行开始,然后继续到下一个 textarea,基本上是沿着行向下。
EDIT Based on answers, I used the following code to traverse the textareas row by row:
编辑根据答案,我使用以下代码逐行遍历 textareas:
var curElt = $('#' + startAt); //startAt is the first row id
for(var i=1; i < 10; i++) {
$(curElt).find('textarea').eq(0).val(i);
$(curElt).find('textarea').eq(1).val(i+1);
curElt = $(curElt).next();
}
回答by Jacob Relkin
You can use the next
and find
methods:
$('#' + current_row_id).next().find('textarea').eq(0);
next
will get the next sibling, and then find
will find all of the elements within the set that match the passed-in selector(s), then eq(0)
will grab the first element in the resulting set returned from find
.
next
将获得下一个同级,然后find
将找到与传入选择器匹配的集合中的所有元素,然后eq(0)
将获取从 返回的结果集中的第一个元素find
。
回答by Phrogz
$('#rowId2').find('textarea');
That will find both children of the row. If you want the first one, either:
这将找到该行的两个孩子。如果你想要第一个,要么:
$('#rowId2').find('textarea').eq(0); //jQuery object
$('#rowId2').find('textarea')[0]; //DOM Element
Edit: If you only know one row and want to find the first textarea in the next row:
编辑:如果您只知道一行并想在下一行中找到第一个 textarea:
$('#rowId').next().find('textarea').eq(0);
回答by draeton
$textarea = $('#rowId textarea').eq(0);
$nextarea = $textarea.closest('tr').next().find('textarea').eq(0);
Just to clarify, $.fn.next() finds the next siblingin the DOM.
澄清一下,$.fn.next()在 DOM 中查找下一个兄弟。
- Starting from the textarea, first you have to find its parent-tr ( $textarea.closest('tr') ).
- From there, use next to find the next tr ( .next() )
- Finally, find the first textarea within thattr ( .find('textarea').eq(0) )
- 从 textarea 开始,首先你必须找到它的 parent-tr ( $textarea.closest('tr') )。
- 从那里,使用 next 找到下一个 tr ( .next() )
- 最后,发现内的第一个textarea的是TR(.find( 'textarea的')。EQ(0))
回答by Chandu
Try this:
尝试这个:
$("#rowID textarea:first-child").val()