如何使用jQuery将数据插入最接近的td? [英] how to insert data to a closest td with jQuery?
本文介绍了如何使用jQuery将数据插入最接近的td?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图获取input textbox
的值并将该值插入同一行的td中.
I am trying to get value of an input textbox
and insert the value into a td in the same row.
这是我的HTML代码:
Here is my HTML Code:
<table class="test">
<tr ><td class="x"><input type="text" class="testinp"></td><td class="y">Fish</td><td class="z">Chicken</td></tr>
<tr ><td class="x"><input type="text" class="testinp"></td><td class="y">Cricket</td><td class="z">Tenis</td></tr>
<tr ><td class="x"><input type="text" class="testinp"></td><td class="y">Watch</td><td class="z">Hat</td></tr>
</table>
这是我的jQuery代码:
Here is my jQuery code:
$('.testinp').keypress(function(e){
if(e.which == 13){
var val = $(this).val();
$(this).find('td').closest('.z').html(val);
}
});
现在我正在获取值,但无法将其插入到td中.
Now I am getting the value but cannot insert it into the td.
推荐答案
.find()
看起来input
的后代中的selector
,而不是父项.因此,您需要使用$(this).closest('td')
.
.find()
looks for the selector
in the descendants of input
, not the parents. So you need to use $(this).closest('td')
.
然后,您可以像这样使用 .siblings()
:
Then, you can use .siblings()
like this:
$(this).closest('td').siblings('td.z').text(val);
或
获取最近的tr
,然后查找具有z
类的td
:
get the nearest tr
, and look for a td
with z
class:
$(this).closest('tr').find('td.z').text(val);
这篇关于如何使用jQuery将数据插入最接近的td?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文