简体   繁体   中英

jQuery - Find the next element which has a specific child

Lets say I have something like this:

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

I am in the event handler for a keypress in the first text box. I want to find the next td which has a text box in it if it exists using jQuery.

Something like this should work (assuming this is the input).

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

tj111's answer does not work for me. May be that is because of newer version of jQuery. I came up with this:

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

I don't know what is your selector, but if you need to select all inputs (type text). You can try this.

$(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

So, do not matter where you put more inputs, you can always take them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM