[英]How can I populate a dropdown list excluding any one option from another dropdown list in javascript?
I have three drop down lists labelled as Subject Choice 1, Subject Choice 2 and Subject Choice 3. Only the first Subject list is populated upon the web page loading. 我有三个下拉列表,分别标记为“主题选择1”,“主题选择2”和“主题选择3”。只有第一个“主题”列表会在加载网页时填充。 How can I populate the second list on changing the first and the third list on changing the second list?
在更改第一个列表时如何填充第二个列表,在更改第二个列表时如何填充第三个列表? Also another requirement is that each list to be populated should be mutually exclusive.
另外一个要求是,要填充的每个列表应互斥。
Lets have an example: initially the Second list and Third List are void. 让我们举个例子:最初,第二列表和第三列表是无效的。 Say The first list contains:
说第一个列表包含:
Now the user selects say 3) Mathematics, then the second list will be 现在用户选择说3)数学,那么第二个列表将是
This time if the user selects say 2), then the third list will be 这次,如果用户选择说2),则第三个列表将是
I am looking for the whole thing to be done using pure JavaScript or jQuery if absolutely needed, but no database interaction. 我正在寻找绝对需要使用纯JavaScript或jQuery完成的全部工作,但没有数据库交互。
<!-- javascript code for dynamic dropmenu For Hons 2 Subject -->
function AjaxFunctionHonsSub2(hons_sub_id2)
{
var httpxml;
try
{
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
function stateck()
{
if(httpxml.readyState==4)
{
var myarray=eval(httpxml.responseText);
// Before adding new we must remove previously loaded elements
for(j=document.applicationform.hons_pref_sub3.options.length-1;j>=0;j--)
{
document.applicationform.hons_pref_sub3.remove(j);
}
// add option for subject 3
var index=0;
var optn2 = document.createElement("OPTION");
optn2.value = '19';
optn2.text = '------SELECT------';
document.applicationform.hons_pref_sub3.options.add(optn2);
for (i=0;i<myarray.length;i++)
{
if(index<myarray.length){
var optn = document.createElement("OPTION");
optn.value = myarray[index];
optn.text = myarray[index+1];
document.applicationform.hons_pref_sub3.options.add(optn);
index+=2;
}
}
}
}
var url="fetch_hons_subject.php";
url=url+"?hons_sub_id="+hons_sub_id2;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
}
<!--# end javascript code for dynamic dropmenu ======================================================================-->
Please see the following code. 请参见以下代码。 It is not a very elegant approach but it works fine.
这不是一个非常优雅的方法,但是效果很好。 I have implemented it for the second dropdown, you can implement similarly for the third dropdown as well.
我已经为第二个下拉列表实现了它,您也可以为第三个下拉列表类似地实现。
<html>
<head>
<script>
function changeDD2()
{
var ddTemp = document.getElementById('mydropdown');
var dd2Temp = document.getElementById('mydropdown2');
var ddSelectedIndex = ddTemp.selectedIndex;
if(ddSelectedIndex==0)
{
alert("Please select valid input");
return;
}
else
{
for(var i=1;i<ddTemp.length;i++)
{
if(i!=ddSelectedIndex)
{
var opt = ddTemp[i];
var el = document.createElement("option");
el.textContent = opt.text;
el.value = opt.value;
dd2Temp.appendChild(el);
}
}
}
}
</script>
</head>
<body>
<select id="mydropdown" name = "mydropdown" onChange="changeDD2()">
<option>None</option>
<option>Maths</option>
<option>Bio</option>
<option>Physics</option>
<option>Chemistry</option>
</select>
<select id="mydropdown2" name = "mydropdown2">
</select>
<select id="mydropdown3" name = "mydropdown3">
</select>
</body>
</html>
A lot of things could be generically/dynamically driven here avoiding the use of the hard code. 这里可以避免通用代码的大量/动态驱动,而无需使用硬代码。
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.