简体   繁体   English

单击时切换下拉菜单,使用 jquery 隐藏其他人

[英]toggle dropdown on click, hide others using jquery

Not sure why this does not work but I have 3 different dropdowns built, an element to click to trigger the dropdown, all is working as expected.不知道为什么这不起作用,但我构建了 3 个不同的下拉菜单,一个单击以触发下拉菜单的元素,一切都按预期工作。 My issue is I want to hide all the others when one is clicked, I have this working to a degree, yes they are all hiding when I click the one I want open but now when I click that same one to "close" or "toggle off" it remains open?我的问题是我想在单击一个时隐藏所有其他人,我在一定程度上可以工作,是的,当我单击我要打开的那个时它们都隐藏了,但是现在当我单击同一个“关闭”或“关闭”它仍然打开? How can I ensure the second click closes the current dropdown?如何确保第二次单击关闭当前下拉菜单? much appreciated if anyone can help me out with this one如果有人能帮我解决这个问题,我将不胜感激

HTML: HTML:

<div class = "dropdown-input-div">Click to Trigger open this one</div>

<ul class="form-dropdown-ul opened">
    <li class=" dropdown-list-item" id="list-item-0">
        <label class="dropdown-label">0-2 years ago</label></li>
    <li class="dropdown-list-item" id="list-item-1">
        <label class="dropdown-label">3-5 years ago</label></li>
    <li class="dropdown-list-item" id="list-item-2">
        <label class=" dropdown-label">5-8 years ago</label></li>
    <li class="dropdown-list-item" id="list-item-3">
        <label class="dropdown-label">8-10 years ago</label></li>
</ul>



<div class = "dropdown-input-div">Click to Trigger open this one</div>

<ul class="form-dropdown-ul">
    <li class=" dropdown-list-item" id="list-item-4">
        <label class="dropdown-label">10-12 years ago</label></li>
    <li class="dropdown-list-item" id="list-item-5">
        <label class="dropdown-label">12-16 years ago</label></li>
    <li class="dropdown-list-item" id="list-item-6">
        <label class=" dropdown-label">16-20 years ago</label></li>
</ul>



<div class = "dropdown-input-div">Click to Trigger open this one</div>

<ul class="form-dropdown-ul">
    <li class=" dropdown-list-item" id="list-item-4">
        <label class="dropdown-label">10-12 years ago</label></li>
    <li class="dropdown-list-item" id="list-item-5">
        <label class="dropdown-label">12-16 years ago</label></li>
    <li class="dropdown-list-item" id="list-item-6">
        <label class=" dropdown-label">16-20 years ago</label></li>
</ul>

jQuery: jQuery:

  $('.form-dropdown-ul').hide();

$('.dropdown-input-div').on('click',function(){
  $('.form-dropdown-ul').hide();
  $(this).next('.form-dropdown-ul').toggle();
});

Place $('ul').hide() before your click function to hide all the ul elements.在单击 function 之前放置$('ul').hide()以隐藏所有ul元素。 Then when you trigger this on the elements next ul , it will only toggle that elements child.然后,当您next ul元素上触发this时,它只会切换该元素的子元素。 Hide all siblings of the selected elements next().siblings('ul') .隐藏所选元素的所有兄弟元素next().siblings('ul')

Add: $(this).next().siblings("ul").hide();添加: $(this).next().siblings("ul").hide();

 $(document).ready(function() { $("ul").hide();//<-- initially hide the UL elements $(".dropdown-input-div").click(function() { $(this).next("ul").toggle(); //<-- toggle the next ul element of the selected DIV $(this).next().siblings("ul").hide(); //<-- hide all siblings of the selected ul }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="dropdown-input-div">Click to Trigger open this one</div> <ul class="form-dropdown-ul opened"> <li class=" dropdown-list-item" id="list-item-0"> <label class="dropdown-label">0-2 years ago</label></li> <li class="dropdown-list-item" id="list-item-1"> <label class="dropdown-label">3-5 years ago</label></li> <li class="dropdown-list-item" id="list-item-2"> <label class=" dropdown-label">5-8 years ago</label></li> <li class="dropdown-list-item" id="list-item-3"> <label class="dropdown-label">8-10 years ago</label></li> </ul> <div class="dropdown-input-div">Click to Trigger open this one</div> <ul class="form-dropdown-ul"> <li class="dropdown-list-item" id="list-item-4"> <label class="dropdown-label">10-12 years ago</label></li> <li class="dropdown-list-item" id="list-item-5"> <label class="dropdown-label">12-16 years ago</label></li> <li class="dropdown-list-item" id="list-item-6"> <label class=" dropdown-label">16-20 years ago</label></li> </ul> <div class="dropdown-input-div">Click to Trigger open this one</div> <ul class="form-dropdown-ul"> <li class=" dropdown-list-item" id="list-item-4"> <label class="dropdown-label">10-12 years ago</label></li> <li class="dropdown-list-item" id="list-item-5"> <label class="dropdown-label">12-16 years ago</label></li> <li class="dropdown-list-item" id="list-item-6"> <label class=" dropdown-label">16-20 years ago</label></li> </ul>

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

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