简体   繁体   English

JavaScript:动态下拉菜单用于另一个隐藏的下拉菜单和其他选项

[英]JavaScript : Dynamic Dropdown for Another Hidden Dropdown and Different Option

I have a dropdown named Agency with 2 options : 我有一个名为Agency的下拉菜单,其中有2个选项:

  • SPM SPM
  • PPS PPS

If SPM are chosen, no dropdown will be appear. 如果选择了SPM,则不会显示任何下拉列表。 But if PPS are chosen, another 2 different dropdown named Department and Office will appear. 但是,如果选择了PPS,则会出现另外两个名为Department and Office的下拉菜单。 In Department , it will be another 3 options : Department中 ,这将是另外三个选项:

  • HQ HQ
  • Branch
  • Stall 摊子


If HQ are chosen, option null will be shown in Office dropdown. 如果选择了总部 ,则选项null将显示在Office下拉列表中。 But if Branch are chosen, Office dropdown will show option such as : 但是,如果选择“ 分支” ,则Office下拉列表将显示以下选项:

  • Floor 1 1楼
  • Floor 2 2楼

In the other side, if Stall are chosen, Office dropdown will show option such as : 另一方面,如果选择“ 停转” ,则“ Office”下拉列表将显示以下选项:

  • Floor 3 3楼
  • Floor 4 4楼


<!-- <script>
var created = 0;

        function displayAccordingly() {

            if (created == 1) {
                removeDrop();
            }

            //Call Agency the main dropdown menu
            var Agency = document.getElementById('Agency');

            //Create the new dropdown menu
            var whereToPut = document.getElementById('myDiv');
            var newDropdown = document.createElement('select');
            newDropdown.setAttribute('id',"newDropdownMenu");
            whereToPut.appendChild(newDropdown);

            if (Agency.value == "PPS") {

                    var optionD=document.createElement("option");
                optionD.text="Department";
                newDropdown.add(optionD,newDropdown.options[null]);

                var optionOffice=document.createElement("option");
                optionOffice.text="Office";
                newDropdown.add(optionOffice,newDropdown.options[null]);

            } else if (Agency.value == "SPM") { 

                removeDrop('myDiv').hide();
            }

            created = 1

            }

            function removeDrop() {
            var d = document.getElementById('myDiv');
            var oldmenu = document.getElementById('newDropdownMenu');
            d.removeChild(oldmenu);
        }

</script>
 -->

<!DOCTYPE html>
<html>
<head>
<title>Administration</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  


<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#Agency").change(function(){

//To remove all the dropdown in myDiv
$('#myDiv').html('');

//Create the new dropdown menu
var whereToPut1 = document.getElementById('myDiv');//Department dropdown
var newDropdown1 = document.createElement('select');
newDropdown1.setAttribute('id',"newDropdownMenu");
whereToPut1.appendChild(newDropdown1);

//Create another div inside myDiv for another dropdown menu
var innerDropdowndiv = document.createElement('div');
innerDropdowndiv.setAttribute('id',"innerdropdowndiv");
whereToPut1.appendChild(innerDropdowndiv);
var whereToPut2 = document.getElementById('innerdropdowndiv');

//Create another dropdown menu
var newDropdown2 = document.createElement('select');//Office dropdown
newDropdown2.setAttribute('id',"innerDropdownMenu");
whereToPut2.appendChild(newDropdown2);

if ($('#Agency').find(":selected").text() == "PPS") {
                $('#newDropdownMenu').append('<option id="Department">Department</option>')//Department dropdown appear
                $('#newDropdownMenu').append('<option>Office</option>')//Office dropdown will appear below Department
                $('#innerDropdownMenu').append('<option>HQ</option>')
                $('#innerDropdownMenu').append('<option>Branch</option>')
                $('#innerDropdownMenu').append('<option>Stall</option>')

                $("#newDropdownMenu").change(function(){
                    if($('#newDropdownMenu').find(":selected").text() == "Department")
                    {
                      $('#innerdropdowndiv').html('');
                      $('#innerdropdowndiv').append($('<select>').attr('id','innerDropdownMenu'))
                      $('#innerDropdownMenu').append('<option>HQ</option>')
                      $('#innerDropdownMenu').append('<option>Branch</option>')
                      $('#innerDropdownMenu').append('<option>Stall</option>')
                    }
                    else if($('#newDropdownMenu').find(":selected").text() == "Office" && $('#innerDropdownMenu').find(":selected").text()=="Branch")
                    {
                      $('#innerdropdowndiv').html('');
                      $('#innerdropdowndiv').append($('<select>').attr('id','innerDropdownMenu'))
                      $('#innerDropdownMenu').append('<option>Floor1</option>')
                      $('#innerDropdownMenu').append('<option>Floor2</option>')
                    }
                    else if($('#newDropdownMenu').find(":selected").text() == "Office" && $('#innerDropdownMenu').find(":selected").text()=="Stall")
                    {
                      $('#innerdropdowndiv').html('');
                      $('#innerdropdowndiv').append($('<select>').attr('id','innerDropdownMenu'))
                      $('#innerDropdownMenu').append('<option>Floor3</option>')
                      $('#innerDropdownMenu').append('<option>Floor4</option>')
                    }
                    else {
                      {
                        $('#innerdropdowndiv').html('');
                      }
                    }
                })
              }
else {
      $('#myDiv').html('');
}
});
});

</script>
</head>
<body>
<td class = "Agency">Agency<span class="required">&nbsp; * &nbsp;&nbsp;&nbsp;</span></td>
     <td><select id="Agency"">
        <option value="Choose">Choose</option>
        <option value="SPM">SPM</option>
        <option value="PPS">PPS</option>
        </select>
        <h></h>
        </td>
         <tr>
      <td></td>
     <td>
        <div id="myDiv"></div>
       </td>
     </tr>
     <td>
        <div id="innerdropdowndiv"></div>
       </td>
     </tr>
    </body>
     </html>
<script>
$(document).ready(function(){
$("#Agency").change(function(){

//To remove all the dropdown in myDiv
$('#myDiv').html('');

//Create the new dropdown menu
var whereToPut1 = document.getElementById('myDiv');
var newDropdown1 = document.createElement('select');
newDropdown1.setAttribute('id',"newDropdownMenu");
whereToPut1.appendChild(newDropdown1);

//Create another div inside myDiv for another dropdown menu
var innerDropdowndiv = document.createElement('div');
innerDropdowndiv.setAttribute('id',"innerdropdowndiv");
whereToPut1.appendChild(innerDropdowndiv);
var whereToPut2 = document.getElementById('innerdropdowndiv');

//Create another dropdown menu
var newDropdown2 = document.createElement('select');
newDropdown2.setAttribute('id',"innerDropdownMenu");
whereToPut2.appendChild(newDropdown2);

if ($('#Agency').find(":selected").text() == "PPS") {
                $('#newDropdownMenu').append('<option id="Dep">Department</option>')
                $('#newDropdownMenu').append('<option>Office</option>')
                $('#innerDropdownMenu').append('<option>HQ</option>')
                $('#innerDropdownMenu').append('<option>Branch</option>')
                $('#innerDropdownMenu').append('<option>Stall</option>')

                $("#newDropdownMenu").change(function(){
                    if($('#newDropdownMenu').find(":selected").text() == "Department")
                    {
                      $('#innerdropdowndiv').html('');
                      $('#innerdropdowndiv').append($('<select>').attr('id','innerDropdownMenu'))
                      $('#innerDropdownMenu').append('<option>HQ</option>')
                      $('#innerDropdownMenu').append('<option>Branch</option>')
                      $('#innerDropdownMenu').append('<option>Stall</option>')
                    }
                    else if($('#newDropdownMenu').find(":selected").text() == "Office" && $('#innerDropdownMenu').find(":selected").text()=="Branch")
                    {
                      $('#innerdropdowndiv').html('');
                      $('#innerdropdowndiv').append($('<select>').attr('id','innerDropdownMenu'))
                      $('#innerDropdownMenu').append('<option>Floor1</option>')
                      $('#innerDropdownMenu').append('<option>Floor2</option>')
                    }
                    else if($('#newDropdownMenu').find(":selected").text() == "Office" && $('#innerDropdownMenu').find(":selected").text()=="Stall")
                    {
                      $('#innerdropdowndiv').html('');
                      $('#innerdropdowndiv').append($('<select>').attr('id','innerDropdownMenu'))
                      $('#innerDropdownMenu').append('<option>Floor3</option>')
                      $('#innerDropdownMenu').append('<option>Floor4</option>')
                    }
                    else {
                      {
                        $('#innerdropdowndiv').html('');
                      }
                    }
                })
              }
else {
      $('#myDiv').html('');
}
});
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁用动态下拉选项值 onchange another dynamic dropdown - Disable dynamic dropdown option value onchange another dynamic dropdown JavaScript:根据另一个下拉列表从下拉列表中删除不同的选项标签? - Javascript: Remove different option-tags from a dropdown list depending on another dropdown? 另一个下拉菜单的自动选择下拉菜单选项 - Autoselect dropdown option of another dropdown Javascript 下拉列表 - 根据同一下拉列表中的选定选项显示不同的选项 - Javascript dropdown - show different options based on selected option in the same dropdown 如何通过使用javascript在另一个下拉菜单中进行选择来禁用下拉菜单中的选项 - How to disable the option in dropdown by selecting in another dropdown using javascript 如何在下拉菜单中有隐藏选项? - How to have a hidden option in a dropdown? 从下拉列表中选择选项时,Javascript显示隐藏的div - Javascript to show hidden div when option is selected from dropdown list 基于另一个下拉列表的动态文本框和下拉列表-javascript - Dynamic textbox and dropdown list based on another dropdown list -javascript 在更改另一个下拉选项时隐藏下拉选项 - hide dropdown option on change of another dropdown option 下拉选择的选项在另一个下拉菜单中选择选项 - dropdown selected option picks option in another dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM