简体   繁体   English

使用 jquery 仅选择同一 div 中的元素

[英]selecting only the element in the same div with jquery

how can i selec only the element in the same div with jquery我怎样才能用 jquery 只选择同一 div 中的元素

 $('.to-show').hide(); $('#offset').on('change', function() { if ($(this).val() == "Custom") { $(this).closest('.wrapper').children('.to-show').toggle(); } else { $('.to-show').hide(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <div class="to-click"> <td class="value"> <select id="offset" class="" name="attribute_offset" data-attribute_name="attribute_offset" data-show_option_none="yes"> <option value="">Choose an option</option> <option value="15" selected="selected" class="attached enabled">15</option> <option value="Custom" class="attached enabled">Custom</option> </select> </td> </div> <div class="to-show"> <div class="tc-cell tc-col-auto"><button type="button" class="button tm-section-link" data-title="Custom Offset" data-sectionid="61cc25f8bfe870.59019822">Custom Offset</button></div> </div> </div> <div class="wrapper"> <div class="to-click"> <td class="value"> <select id="offset" class="" name="attribute_offset" data-attribute_name="attribute_offset" data-show_option_none="yes"> <option value="">Choose an option</option> <option value="15" selected="selected" class="attached enabled">15</option> <option value="Custom" class="attached enabled">Custom</option> </select> </td> </div> <div class="to-show"> <div class="tc-cell tc-col-auto"><button type="button" class="button tm-section-link" data-title="Custom Offset" data-sectionid="61cc25f8bfe870.59019822">Custom Offset</button></div> </div> </div>

So this only work with first select option所以这只适用于第一个 select 选项

How i can make it work with the second select option what i am missing here?我如何使它与第二个 select 选项一起工作我在这里缺少什么?

i tried also $(this).closest('.wrapper').find('.to-show').toggle();我也试过$(this).closest('.wrapper').find('.to-show').toggle();

but that did not work但这没有用

thank you谢谢你

Having two elements with the same id is invalid html.具有相同 id 的两个元素是无效的 html。 Switch it to use class or another attribute.将其切换为使用 class 或其他属性。 But you have a second problem with $('.to-show').hide();但是你有第二个问题$('.to-show').hide(); , it is always hiding both .to-show . ,它总是隐藏两个.to-show

 $('.to-show').hide(); $('.offset').on('change', function() { if ($(this).val() == "Custom") { $(this).closest('.wrapper').children('.to-show').toggle(); } else { $(this).closest('.wrapper').children('.to-show').hide(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="wrapper"> <div class="to-click"> <td class="value"> <select id="offset" class="offset" name="attribute_offset" data-attribute_name="attribute_offset" data-show_option_none="yes"> <option value="">Choose an option</option> <option value="15" selected="selected" class="attached enabled">15</option> <option value="Custom" class="attached enabled">Custom</option> </select> </td> </div> <div class="to-show"> <div class="tc-cell tc-col-auto"><button type="button" class="button tm-section-link" data-title="Custom Offset" data-sectionid="61cc25f8bfe870.59019822">Custom Offset</button></div> </div> </div> <div class="wrapper"> <div class="to-click"> <td class="value"> <select id="offset" class="offset" name="attribute_offset" data-attribute_name="attribute_offset" data-show_option_none="yes"> <option value="">Choose an option</option> <option value="15" selected="selected" class="attached enabled">15</option> <option value="Custom" class="attached enabled">Custom</option> </select> </td> </div> <div class="to-show"> <div class="tc-cell tc-col-auto"><button type="button" class="button tm-section-link" data-title="Custom Offset" data-sectionid="61cc25f8bfe870.59019822">Custom Offset</button></div> </div> </div>

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

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