简体   繁体   English

获取当前元素父级,然后每个元素jQuery

[英]get current element parent then each element jquery

I want to get current element parent, then for each check the class here is my code: 我想让当前元素成为父元素,然后对于每个检查,这里的类都是我的代码:

 jQuery('input[name^="opt"]').click(function() { if ( jQuery(this).hasClass('current_option') && jQuery(this).is(':checked') ) { jQuery(this).prop('checked', false); jQuery(this).removeClass('current_option'); opConfig.reloadPrice(); } else { var parents=jQuery(this).parent().parent(); jQuery(parents +'li > input').each(function() { alert("445"); }); jQuery(this).addClass('current_option'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul> <li><input type="radio" value="1" name="opt[1]" /><li> <li><input type="radio" value="1" name="opt[1]" /><li> <li><input type="radio" value="1" name="opt[1]" /><li> <li><input type="radio" value="1" name="opt[1]" /><li> </ul> <ul> <li><input type="radio" value="1" name="opt[2]" /><li> <li><input type="radio" value="1" name="opt[2]" /><li> <li><input type="radio" value="1" name="opt[2]" /><li> <li><input type="radio" value="1" name="opt[2]" /><li> </ul> <ul> <li><input type="radio" value="1" name="opt[3]" /><li> <li><input type="radio" value="1" name="opt[3]" /><li> <li><input type="radio" value="1" name="opt[3]" /><li> <li><input type="radio" value="1" name="opt[3]" /><li> </ul> 

This part of code is not working. 这部分代码不起作用。 I have to remove the class for current parent other elements. 我必须删除当前父类其他元素的类。

var parents=jQuery(this).parent().parent();
jQuery(parents +'li > input').each(function() {
  alert("445");
});

can you explain why you used this part of snippet ? 您能解释一下为什么使用了这段代码吗?

var parents=jQuery(this).parent().parent();
jQuery(parents +'li > input').each(function() {
  alert("445");
});

If you explain little-bit then some one maybe give the right idea to best way to do it. 如果您解释一点点,那么可能有人会给出正确的想法,以最好的方式做到这一点。 and one more thing what really you want to do with your full of code ? 还有一件事,您真正想用全部代码做什么?

Anyway you can't get li > input by parents variable that you store by using parent() function. 反正你不能让li > inputparents变量您存储使用parent()函数。

why don't you try find() to this for you ? 您为什么不尝试使用find()呢? like : 喜欢 :

$(parents).find('input').each(function() {
  alert("445");
});

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

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