简体   繁体   English

当用户从上一个元素中选择一个选项时,如何显示隐藏的元素

[英]How to show a hidden element when a user selects an option from a previous element

I'm creating a form for an assignment. 我正在为作业创建表格。 How am I able to make a new question appear upon the selection on a value on a different question? 在选择其他问题的值时,如何使新问题出现? For example, 例如,

<label for="Etype">*What would you like to enrol in?</label>
<select id="Etype" name="Etype">
    <option value="">Select...</option>
    <option value="Camp">Camp</option>
    <option value="Class">Class</option>
</select>
</fieldset>

and upon the selection of "class" a previously hidden question is revealed beneath the previous question asking "type of class?" 在选择“班级”之后,在先前询问“班级类型”的问题下方会显示一个先前隐藏的问题。

Put this just before your body closing tag: 将其放在您的身体闭合标签之前:

<script>
document.getElementById('Etype').onchange = function() {
    var isSelected = this.options[this.selectedIndex].value == 'Class';
    document.getElementById('hiddenField').style.display = isSelected ? 'block':'none';
};
</script>

Assuming the hidden element has an ID 'hiddenField': 假设隐藏元素的ID为“ hiddenField”:

<div id="hiddenField" style="display:none;">
    Place the hidden field, labels, etc. here
</div>

HTML 的HTML

<fieldset>
<label for="Etype">*What would you like to enrol in?</label>
       <select id="Etype" onchange="show_hidden(this.value)" name="Etype">
          <option value="">Select...</option>
          <option value="Camp">Camp</option>
          <option value="Class">Class</option>
      </select>

<div id="class">
Type of class : <input type="text" name="class_text" />
</div><!-- #class -->
<div id="camp">
 Type of Camp <input type="text" name="camp_text" />
</div><!-- #camp -->
</fieldset>

CSS 的CSS

#class, #camp{display:none;}

Javascript Java脚本

function show_hidden(str)
{
   // if first option was selected, hide both the hidden fields
   if(str == '')
   {
      document.getElementById('class').style.display = 'none';
      document.getElementById('camp').style.display = 'none';
   }
   // if class was selected, show the class fields and hide camp fields if visible
   else if(str == 'Class')
   {
      document.getElementById('class').style.display = 'block';
      document.getElementById('camp').style.display = 'none';
   }
   // if camp was selected, show the camp fields and hide class fields if visible
   else if(str == 'Camp')
   {
      document.getElementById('camp').style.display = 'block';
      document.getElementById('class').style.display = 'none';
   }
}

DEMO 演示

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

相关问题 当用户选择一个选项,导航或选择相同的选项时,如何隐藏元素? - How can I hide an element when a user selects an option, navigates away, or selects the same option? 当用户在 select 元素中选择一个选项时,如何在 object 数组中显示 object 的值? - how do I display the values of an object in object array when user selects an option in select element? 根据选择选项元素中选择的选项显示隐藏的div元素 - Show hidden div element based on option selected in select option element 如果用户从选择框中选择一个选项,隐藏的 div 应该出现 - If a user selects a option from the select box, hidden div should appear 用户从列表中选择一个选项而不单击提交按钮后,如何在页面中显示文本框或选择/选项? - How to show a textbox or select/option in the page once user selects an option from the list without clicking the submit button? 从列表中选择新选项时,将其替换上一个元素 - When selecting new option from list have it replace previous element 如何使用 AngularJS 显示隐藏元素 - How to show hidden element with AngularJS 如何使用 jQuery 显示隐藏元素 - How to show a hidden element with jQuery 用户从列表中选择时如何显示内容详细信息? - How to show content details when user selects from list? 隐藏元素时如何从源中隐藏元素? - How to hide an element from the source when it is hidden?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM