简体   繁体   English

根据下拉选择值显示/隐藏元素

[英]Show/hide element based on dropdown selection value

I have a dropdown menu for selecting a filter for the frontpage content. 我有一个下拉菜单,用于为首页内容选择过滤器。 I want it so that if the selected value is "top" a second dropdown appears next to it with the timeframes. 我想要这样,以便如果选择的值是“ top”,则带有时间范围的第二个下拉菜单会出现在它旁边。 I have the HTML and CSS for both of them, but can't get the JQuery working. 我都有它们的HTML和CSS,但是无法使JQuery正常工作。

What I tried to do with the JQuery is like the following: 我尝试对JQuery进行的操作如下所示:

if (.order value == "top") {
    change .order-time to .order-time-display;
}

 .order-container { margin: 0 auto; width: 75%; margin-bottom: -10px; } .order-text { display: inline-block; } .order-menu { margin-left: 10px; display: inline-block; } .order-container select { padding: 14px 16px; font-size: 14px; } .order-container p { font-weight: bold; } .order-container option { font-size: 14px; } .order-time { display: none; } .order-time-display { display: inline-block; } 
 <div class="order-container"> <div class="order-text"> <p>Order:</p> </div> <div class="order-menu"> <select name="order" class="order"> <option value="hot" selected> Hot </option> <option value="recent"> Recent </option> <option value="top"> Top </option> </select> </div> <select name="order-time" class="order-time"> <option value="24h" selected> 24h </option> <option value="week"> Week </option> <option value="month"> Mont </option> <option value="year"> Year </option> <option value="all"> All </option> </select> </div> 

Use the change event on the .order element. .order元素上使用change事件。

 $(function() { $(".order").on("change", function() { if (this.value === "top") { $(".order-time").addClass("order-time-display") } else { $(".order-time").removeClass("order-time-display") } }) }) 
 .order-container { margin: 0 auto; width: 75%; margin-bottom: -10px; } .order-text { display: inline-block; } .order-menu { margin-left: 10px; display: inline-block; } .order-container select { padding: 14px 16px; font-size: 14px; } .order-container p { font-weight: bold; } .order-container option { font-size: 14px; } .order-time { display: none; } .order-time-display { display: inline-block; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="order-container"> <div class="order-text"> <p>Order:</p> </div> <div class="order-menu"> <select name="order" class="order"> <option value="hot" selected> Hot </option> <option value="recent"> Recent </option> <option value="top"> Top </option> </select> </div> <select name="order-time" class="order-time"> <option value="24h" selected> 24h </option> <option value="week"> Week </option> <option value="month"> Mont </option> <option value="year"> Year </option> <option value="all"> All </option> </select> </div> 

I'm assuming you want to display the second select box order-time based on the first's value where the value should be top to display it .. 我假设您要根据第一个选择的值显示第二个选择框的order-time ,其中该值应显示在top


$(document).on('change', 'select[name=order]', function () {

  let val = $(this).val();
  let orderTimeSelectBox = $('select[name=order-time]')

  if (val === 'top') {

    orderTimeSelectBox.show()
  }
  else {
    orderTimeSelectBox.hide()
  }
})

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

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