简体   繁体   中英

jQuery Locating Element Several Levels Up

I am attempting to locate an input field using the following:

parent = $(this).parent().parent().$('input[type="text"]').attr('id');

However, my script appears to be crashing whenever this runs. Basically, I have a tree of input fields within nested using <ul> 's and <li> 's, and I am trying to access the parent input field of $(this). Any help would be hugely appreciated!

The syntax for your statement is incredibly wrong =D

It sounds like you are looking for the find function:

$(this).parent().parent().find('input[type="text"]').attr('id')

You're probably missing the find function:

parent = $(this).parent().parent().find('input[type="text"]').attr('id');

Maybe this can simplify your code:

parent = $(this).closest('li').find('input[type="text"]').attr('id');

$(this).parent().parent().$('input[type="text"]').attr('id'); is not valid

one possible solution may be

$(this).parent().parent().find('input[type="text"]').attr('id');
parent = $(this).parents('#parentElementID').find('input[type="text"]')[0].id;

其中#parentElementID是目标输入的最接近的父级。

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