Javascript 依赖下拉选择
标准选择Javascript 依赖下拉选择,javascript,ajax,Javascript,Ajax,标准选择 截面选择 选题 取决于选择取决于其他部分取决于标准。主题取决于部分 如果我选择“主题”,则它将显示“仅主题分配”通常,当您需要根据下拉列表更改某些内容时,请使用onchange事件。例如: <select id="standardSelectId" onchange="fillSection(this)">...</select> <select id="sectionSelectId" onchange="fillSubject(this)">&l
如果我选择“主题”,则它将显示“仅主题分配”通常,当您需要根据下拉列表更改某些内容时,请使用onchange事件。例如:
<select id="standardSelectId" onchange="fillSection(this)">...</select>
<select id="sectionSelectId" onchange="fillSubject(this)"><option>-- Select Standard first --</option></select>
<select id="subjectSelectId"><option>-- Select Section first --</option></select>
。。。
--先选择标准--
--首先选择节--
然后,编写一个javascript函数,用选项填充下一个选择,并清除依赖于下一个选择的所有选择:
<script type="text/javascript">
function fillSection(e) {
var choice = e.options[e.selectedIndex];
var sectionSelect = document.getElementById("sectionSelectId");
var subjectSelect = document.getElementById("subjectSelectId");
subjectSelect.options.length = 0;
try { subjectSelect.add(new Option("-- Select Section first --", ""))} catch(ex) {subjectSelect.add(new Option("-- Select Section first --", ""), null)}
sectionSelect.options.length = 0;
switch (choice.value) {
case <standard val1>:
try { sectionSelect.add(new Option(section1_label_1, section1_key_1))} catch(ex) {subjectSelect.add(new Option(section1_label_1, section1_key_1), null)}
...
try { sectionSelect.add(new Option(section1_label_N, section1_key_N))} catch(ex) {subjectSelect.add(new Option(section1_label_N, section1_key_N), null)}
break;
case <standard valX>:
try { sectionSelect.add(new Option(sectionX_label_1, sectionX_key_1))} catch(ex) {subjectSelect.add(new Option(sectionX_label_1, sectionX_key_1), null)}
...
try { sectionSelect.add(new Option(sectionX_label_N, sectionX_key_N))} catch(ex) {subjectSelect.add(new Option(sectionX_label_N, sectionX_key_N), null)}
break;
}
function fillSubject(e) {
...
}
职能部门(e){
var choice=e.options[e.selectedIndex];
var sectionSelect=document.getElementById(“sectionSelectId”);
var subjectSelect=document.getElementById(“subjectSelectId”);
subjectSelect.options.length=0;
请尝试{subjectSelect.add(新选项(“--Select Section first--”和“”)}catch(例如){subjectSelect.add(新选项(“--Select Section first--”和“”),null)}
sectionSelect.options.length=0;
开关(选择值){
案例:
尝试{sectionSelect.add(新选项(section1_标签_1,section1_键_1))}catch(ex){subjectSelect.add(新选项(section1_标签_1,section1_键_1),null)}
...
尝试{sectionSelect.add(新选项(section1\u label\N,section1\u key\N))}catch(ex){subjectSelect.add(新选项(section1\u label\N,section1\u key\N),null)}
打破
案例:
尝试{sectionSelect.add(新选项(sectionX_label_1,sectionX_key_1))}catch(ex){subjectSelect.add(新选项(sectionX_label_1,sectionX_key_1),null)}
...
尝试{sectionSelect.add(新选项(sectionX_label_N,sectionX_key_N))}catch(ex){subjectSelect.add(新选项(sectionX_label_N,sectionX_key_N),null)}
打破
}
函数主题(e){
...
}