简体   繁体   English

如何在javascript中的另一个下拉列表中填充排除任何一个选项的下拉列表?

[英]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: 说第一个列表包含:

  1. Bengali 孟加拉
  2. English 英语
  3. Mathematics 数学
  4. Physics 物理
  5. Chemistry 化学

Now the user selects say 3) Mathematics, then the second list will be 现在用户选择说3)数学,那么第二个列表将是

  1. Bengali 孟加拉
  2. English 英语
  3. Physics 物理
  4. Chemistry 化学

This time if the user selects say 2), then the third list will be 这次,如果用户选择说2),则第三个列表将是

  1. Bengali 孟加拉
  2. Physics 物理
  3. Chemistry 化学

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.

相关问题 如何通过从另一个下拉列表中选择值来填充下拉列表? - How can i populate a dropdown list by selecting the value from another dropdown list? 如何通过从另一个下拉列表中选择值来填充下拉列表 - How can i populate a dropdown list by selecting the value from another dropdown list 从另一个下拉列表中填充下拉列表 - Populate dropdown list from another dropdown list 如何通过从另一个下拉列表中选择值来填充下拉列表? - How to populate a dropdown list by selecting the value from another dropdown list? 根据另一个下拉列表填充一个下拉列表 - Populate one dropdown list based on another dropdown list 如何将值从一个下拉列表存储到另一个下拉列表 - How to store value from one dropdown list to another dropdown list 如果选择了另一个选择下拉列表中的一个选项,如何显示选择下拉列表 - How do I display a select dropdown list when one option on another select dropdown is selected 从另一个下拉列表中选择一个值后填充下拉列表 - Populate dropdown list upon selecting a value from another dropdown list 如何将下拉数据从一个下拉列表填充到另一个下拉列表? - How to populate dropdown data from one dropdown to another dropdown? Angular-如何根据在另一个下拉列表中选择的选项禁用一个下拉列表中的选项? - Angular-How can I disable options from one dropdown based on option selected on another dropdown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM