I have a table with multiple rows, and each row contains three cells. Each cell contains a textbox, so it looks like this:
我有一个包含多行的表,每行包含三个单元格。每个单元格都包含一个文本框,所以它看起来像这样:
XXXX XXXX XXXX
on the keyup event of the first textbox, I want its contents to be copied into the second textbox, but not the third textbox.
在第一个文本框的keyup事件中,我希望将其内容复制到第二个文本框中,而不是第三个文本框。
my keyup event I can get a reference to the first textbox. Doing .parent() will give me the cell, and if I want, doing .parent() again will give me the row.
我的keyup事件我可以获得对第一个文本框的引用。做.parent()会给我单元格,如果我想,再做.parent()会给我一行。
What JQuery can I use to get the adjacent textbox?
我可以使用什么JQuery来获取相邻的文本框?
3 个解决方案
#1
31
You can use .next()
to get the next sibling, like this:
您可以使用.next()来获取下一个兄弟,如下所示:
var nextTD = $(this).closest("td").next();
//for your case:
$(this).closest("td").next().find("input").val($(this).val());
.parent()
works too, .closest()
is just a bit more flexible, for you could change your markup and it'd still go to the nearest <td>
parent.
.parent()也可以工作,.closest()只是更灵活一点,因为你可以改变你的标记,它仍然会转到最近的父级。
#2
3
From the .parent()
<td>
, use next()
to go to the next <td>
, then .children('input')
to get the child <input>
element.
从.parent(),使用next()转到下一个,然后使用.children('input')来获取子元素。
So you end up with something like this in your keyup
handler.
所以你最终会在你的keyup处理程序中得到类似的东西。
$(this).parent().next().children('input').val( this.value );
#3
0
Give the next()
function a try. This selects the next sibling of the selected element.
尝试使用next()函数。这将选择所选元素的下一个兄弟。
#1
31
You can use .next()
to get the next sibling, like this:
您可以使用.next()来获取下一个兄弟,如下所示:
var nextTD = $(this).closest("td").next();
//for your case:
$(this).closest("td").next().find("input").val($(this).val());
.parent()
works too, .closest()
is just a bit more flexible, for you could change your markup and it'd still go to the nearest <td>
parent.
.parent()也可以工作,.closest()只是更灵活一点,因为你可以改变你的标记,它仍然会转到最近的父级。
#2
3
From the .parent()
<td>
, use next()
to go to the next <td>
, then .children('input')
to get the child <input>
element.
从.parent(),使用next()转到下一个,然后使用.children('input')来获取子元素。
So you end up with something like this in your keyup
handler.
所以你最终会在你的keyup处理程序中得到类似的东西。
$(this).parent().next().children('input').val( this.value );
#3
0
Give the next()
function a try. This selects the next sibling of the selected element.
尝试使用next()函数。这将选择所选元素的下一个兄弟。