[英]How can I hide an element when a user selects an option, navigates away, or selects the same option?
[英]How to show a hidden element when a user selects an option from a previous element
我正在為作業創建表格。 在選擇其他問題的值時,如何使新問題出現? 例如,
<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>
在選擇“班級”之后,在先前詢問“班級類型”的問題下方會顯示一個先前隱藏的問題。
將其放在您的身體閉合標簽之前:
<script>
document.getElementById('Etype').onchange = function() {
var isSelected = this.options[this.selectedIndex].value == 'Class';
document.getElementById('hiddenField').style.display = isSelected ? 'block':'none';
};
</script>
假設隱藏元素的ID為“ hiddenField”:
<div id="hiddenField" style="display:none;">
Place the hidden field, labels, etc. here
</div>
<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>
#class, #camp{display:none;}
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';
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.