简体   繁体   English

如何根据下拉菜单用户选择显示内容

[英]How to display content depending on dropdown menue user selection

I have a dropdown menu at the bottom of a form with 6 different options. 我在表单底部有一个下拉菜单,其中包含6个不同的选项。 I need to display different content on a div below this menu depending on which option is selected. 我需要根据选择的选项在此菜单下的div上显示不同的内容。

No additional content should be visible until the user is to select one of the options and once the user select one of the options only content associated with that specific option should be visible. 在用户选择选项之一之前,其他内容均不可见;一旦用户选择选项之一,则仅与该特定选项关联的内容才可见。

I need to create this functionality using JavaScript but my knowledge of JavaScript is very limited and I don't seem to find what I need online. 我需要使用JavaScript创建此功能,但是我对JavaScript的了解非常有限,而且我似乎找不到在线所需的内容。

I believe what I need to do is create 6 different divs(one for each option) and toggle a class that makes them visilble once its respective title is selected. 我相信我需要做的是创建6个不同的div(每个选项一个)并切换一个类,使其在选定其各自的标题后即可显示。

Here is the dropdown menu that I have: 这是我的下拉菜单:

 <div class="field">
    <label for="roleBox" data-label="I_AM">{% trans %} main.I_AM_TITLE {% endtrans %}</label>
    <div class="f-wrapper">
       <select class="cbx" tabindex="50" name="role">
              <option value="student">A Student</option>
              <option value="educator">An Educator</option>
              <option value="parent">A parent signing up for my child</option>
              <option value="not-school">Not in school but still learning</option>
              <option value="publisher">A Publisher or interested Kno partner</option>
       </select>
    </div>
 </div>

Any help will be greatly appreciated. 任何帮助将不胜感激。

I added the div s you mentioned and gave each an id which makes the Javascript a little easier. 我添加了您提到的div并为每个id赋予了一个id ,这使Javascript变得更简单了。

Javascript 使用Javascript

var ids=["student", "educator", "parent", "not-school", "publisher"];
var dropDown = document.getElementById("roleSel");

dropDown.onchange = function(){
    for(var x = 0; x < ids.length; x++){   
        document.getElementById(ids[x]).style.display="none";
    }    
    document.getElementById(this.value).style.display = "block";
}

HTML HTML

 <div class="field">
    <label for="roleBox" data-label="I_AM">{% trans %} main.I_AM_TITLE {% endtrans %}</label>
    <div class="f-wrapper">
       <select id="roleSel" class="cbx" tabindex="50" name="role">
              <option value="student">A Student</option>
              <option value="educator">An Educator</option>
              <option value="parent">A parent signing up for my child</option>
              <option value="not-school">Not in school but still learning</option>
              <option value="publisher">A Publisher or interested Kno partner</option>
       </select>
    </div>
 </div>
<div id="student" class="hidden">Student div</div>
<div id="educator" class="hidden">Educator div</div>
<div id="parent" class="hidden">Student div</div>
<div id="not-school" class="hidden">Not School div</div>
<div id="publisher" class="hidden">Student div</div>

Working Example http://jsfiddle.net/qbzmz/ 工作示例 http://jsfiddle.net/qbzmz/

use the onchange function like so 像这样使用onchange函数

<div class="field">
    <label for="roleBox" data-label="I_AM">{% trans %} main.I_AM_TITLE {% endtrans %}</label>
    <div class="f-wrapper">
       <select class="cbx" id="selctor" tabindex="50" name="role" onchange="selectChanged();">
              <option value="student">A Student</option>
              <option value="educator">An Educator</option>
              <option value="parent">A parent signing up for my child</option>
              <option value="not-school">Not in school but still learning</option>
              <option value="publisher">A Publisher or interested Kno partner</option>
       </select>
    </div>
</div>
<script>
    function selectChanged(){
        //get the value of selector and act upon it
        //i would use jquery to do this stuff
    }
</script>

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

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