简体   繁体   English

选择$(this)元素的父级的同级

[英]Selecting siblings of parent of $(this) element

I need to update the id of all li that are siblings of the parent of the $(this) element by dynamically counting the present li that belong to the same "family" every time one is removed. 我需要通过动态计算每次删除一个属于同一“家庭”的当前li来更新所有$(this)元素的同级liid

Note that there's more than one ul.customFieldUl on the page and more than one li.customFieldLi in each. 请注意,有不止一个ul.customFieldUl在页面上,不止一个li.customFieldLi只。

 $('.customFieldUl').on('click', '.remove-button', function() { var field = $(this).parents('li.customFieldLi'); if (field.length) { field.remove(); // PROBLEMATIC SELECTOR BELOW: $(this).parent().siblings().each(function(i) { var id = i + 1; $(this).prop('id', 'field_' + id); $(this).find('label#top_title').html('Custom Field #: <b>' + id + '</b> &raquo;'); }); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="customFieldUl"> <li class="customFieldLi"> <label id="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> <li class="customFieldLi"> <label id="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> </ul> 

You need to target siblings() of LI before removing the LI element. 在删除LI元素之前,您需要定位LI siblings()

 $('.customFieldUl').on('click', '.remove-button', function() { var field = $(this).closest('li.customFieldLi'); //Take reference of siblings var siblings = field.siblings(); //Remove element field.remove(); // Now iterate and update properties. siblings.each(function(i) { var id = i + 1; $(this).prop('id', 'field_' + id); $(this).find('label#top_title').html('Custom Field #: <b>' + id + '</b> &raquo;'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="customFieldUl"> <li class="customFieldLi"> <label id="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> <li class="customFieldLi"> <label id="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> </ul> 

Identifiers in HTML must be unique , You are using id="top_title" multiple times which renders HTML invalid HTML中的标识符必须唯一 ,您多次使用id="top_title"导致HTML无效

Note: I totally agree with @Rory's comment, changing id attributes dynamically is not a good idea 注意:我完全同意@Rory的评论, 动态更改id属性不是一个好主意

The issue you have is that you're removing the element then trying to select elements based on it. 您遇到的问题是要删除元素,然后尝试基于该元素选择元素。 This won't work as the element no longer exists. 由于该元素不再存在,因此无法使用。 To fix this, get the related elements first, then perform the removal. 要解决此问题,请先获取相关元素,然后执行删除。

Note that you can simplify the code by using the index parameter provided by each() . 请注意,您可以使用each()提供的index参数来简化代码。 Also, you've repeated the top_title id. 另外,您已经重复了top_title ID。 You should make that a class. 你应该把它当作一堂课。 Try this: 尝试这个:

 $('.customFieldUl').on('click', '.remove-button', function() { var $button = $(this); var $ul = $button.closest('.customFieldUl'); $button.closest('.customFieldLi').remove(); $ul.find('.customFieldLi').each(function(i) { $(this).prop('id', 'field_' + i); $(this).find('label.top_title').html('Custom Field #: <b>' + i + '</b> &raquo;'); }) }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="customFieldUl"> <li class="customFieldLi"> <label class="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> <li class="customFieldLi"> <label class="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> <li class="customFieldLi"> <label class="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> <li class="customFieldLi"> <label class="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> <li class="customFieldLi"> <label class="top_title">Additional Field</label> <div class="additionalFieldForm"></div> <div class="remove-button">Remove</div> </li> </ul> 

It's also worth noting that changing id attributes dynamically is not a good idea. 还值得注意的是,动态更改id属性不是一个好主意。 They are meant to be immutable. 它们注定是不变的。

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

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