如果这有帮助,要整理出第一个用例,您可以在 HTML 中执行以下操作:
Age:
<select name='age' id='age' onchange="showRelevantRuleDropdown(this,1);">
<option value='0'>Bronze Age</option>
<option value='1'>Middle Age</option>
...
</select>
<select name='BronzeAge' id='BronzeAge' onchange="showRelevantRuleDropdown(this,1);">
<option value='0'>Household tools</option>
<option value='1'>Jewelry</option>
...
</select>
<select name='MiddleAge' id='MiddleAge' onchange="showRelevantRuleDropdown(this,1);">
<option value='0'>Jewlery</option>
<option value='1'>Religious artifacts</option>
...
</select>
...
<script language='JavaScript'>
showRelevantRuleDropdown(document.getElementById('age'),1);
</script>
使用下面的 Javascript:
function showRelevantRuleDropdown(elem, restore) {
// Show one SELECT element (elem) between Option 1 or Option 2
// If the parameter restore is 1, the hidden element is restored to its default value
if (elem.selectedIndex == 0) {
document.getElementById('BronzeAge').style.display = 'none';
document.getElementById('MiddleAge').style.display = 'inline';
if (restore == 1) {
document.getElementById('BronzeAge').getElementsByTagName('option')['0'].selected = 'selected';
}
}
else {
document.getElementById('MiddleAge').style.display = 'none';
document.getElementById('BronzeAge').style.display = 'inline';
if (restore == 1) {
document.getElementById('MiddleAge').getElementsByTagName('option')['0'].selected = 'selected';
}
}
}
这适用于一层深度,但可以轻松扩展。
如果你想使用它,我有 codepen 中的代码:http: //codepen.io/ophintor/pen/FaHbh