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

时间:2013-08-08 00:17:06

标签: javascript html css

我在表单底部有一个下拉菜单,有6个不同的选项。 我需要在此菜单下方的div上显示不同的内容,具体取决于所选的选项。

在用户选择其中一个选项之前,不应显示其他内容,一旦用户选择其中一个选项,则只有与该特定选项关联的内容应该可见。

我需要使用JavaScript创建此功能,但我对JavaScript的了解非常有限,而且我似乎无法在网上找到我需要的内容。

我相信我需要做的是创建6个不同的div(每个选项一个)并切换 一个类,一旦选择了相应的标题,就可以使它们可见。

以下是我的下拉菜单:

 <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>

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

我添加了您提到的div,并为每个id添加了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"; } ,这使得Javascript更容易。

<强>的Javascript

 <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>

<强> HTML

{{1}}

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

答案 1 :(得分:0)

使用像这样的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>