简体   繁体   English

jQuery定位元素向上几个级别

[英]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). 基本上,我有一棵使用<ul><li>嵌套的输入字段树,并且试图访问$(this)的父输入字段。 Any help would be hugely appreciated! 任何帮助将不胜感激!

The syntax for your statement is incredibly wrong =D 您的语句的语法非常错误= D

It sounds like you are looking for the find function: 听起来您正在寻找find函数:

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

You're probably missing the find function: 您可能缺少find函数:

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是目标输入的最接近的父级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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