2009-03-05 76 views
4

下一個元素可以說我有這樣的事情:jQuery的 - 尋找具有特定的孩子

<tr> 
<td><input type="text" /></td> 
<td>Somevalue</td> 
<td><intput type="text /></td> 
</tr> 

我在第一個文本框中輸入一個按鍵的事件處理程序。我想找到下一個td,如果它存在使用jQuery它有一個文本框。

回答

6

像這樣的東西應該工作(假設this是輸入)。

var next = $(this).parent().next("td > input[type='text']"); 
+0

$(this).parent()。next(「td:has(input [type ='text'])」); 是我結束了,我發現使用>沒有工作,但是:已經做到了。 – 2009-03-05 16:19:41

+0

有趣,很高興它解決了。 – tj111 2009-03-05 16:27:51

-1

我不知道你的選擇器是什麼,但如果你需要選擇所有的輸入(鍵入文本)。你可以試試這個。

$(function(){ 

    $(':input').each(function(){ 
     $(this).keypress(function(){ 
      $(this).next('input[type=text]').val('I\'m the next'); 
    }) // end of keypress 
    }) // enf of each 
}) // End of function 

所以,不要緊,你把更多的投入,你可以隨時把它們。

0

tj111的回答對我無效。 可能是因爲更新版本的jQuery。我想出了這個:

var next = $(this).parent().nextAll().has("input[type='text']").first(); 
相關問題