Cascading Menu with Input

69 views Asked by At

i want to get the cascading between chained selected menu.

It should go to the first dropdown menu when i write a 6 into the Input.

But my problems are, that first i am gettin twice the "c" and that i cant get from the first dropdown menu to the next.

I am thankfull about every help

<html>

<head>
  <style type="text/css">
  </style>

  <script language="Javascript">
    <!-- Start 
    function validateForm() {
      var x = document.forms["myForm"]["fname"].value;
      var speicher;
      var auswahl1 = document.forms.verzeichnis.auswahl1;
      var auswahl2 = document.forms.verzeichnis.auswahl2;
      var auswahl3 = document.forms.verzeichnis.auswahl3;
      auswahl2.options.length = 0; // DropDown Menü entleeren 
      auswahl3.options.length = 0; // DropDown Menü entleeren
      if (x == "6") {
        alert("Name must be filled out");
        auswahl1.options[0] = new Option("a");
        auswahl1.options[1] = new Option("b");
        auswahl1.options[2] = new Option("c");
        auswahl2.options[0] = new Option("---- Bitte waehlen ----");
        auswahl3.options[0] = new Option("---- Bitte waehlen ----");
        return false;
      }



      function update_auswahl1() {
        var speicher;
        var auswahl1 = document.forms.verzeichnis.auswahl1;
        var auswahl2 = document.forms.verzeichnis.auswahl2;
        var auswahl3 = document.forms.verzeichnis.auswahl3;
        auswahl2.options.length = 0; // DropDown Menü entleeren 
        auswahl3.options.length = 0; // DropDown Menü entleeren 

        //********************** AUSWAHL 1 ****************************************************************

        if (auswahl1.options[auswahl1.selectedIndex].value == "a") {
          auswahl2.options[0] = new Option("d");
          auswahl2.options[1] = new Option("e");
        } else if (auswahl1.options[auswahl1.selectedIndex].value == "b") {
          auswahl2.options[0] = new Option("e");
          auswahl2.options[1] = new Option("f");
        } else if (auswahl1.options[auswahl1.selectedIndex].value == "c") {
          auswahl2.options[0] = new Option("f");
          auswahl2.options[1] = new Option("g");
        } else if (auswahl1.options[auswahl1.selectedIndex].value == "") {
          auswahl2.options[0] = new Option("---- Bitte waehlen ----");
        }

        update_auswahl2();
        //*************************************************************************************************
      }
    }

    function update_auswahl2() {
      var speicher;
      var auswahl2 = document.forms.verzeichnis.auswahl2;
      var auswahl3 = document.forms.verzeichnis.auswahl3;
      auswahl3.options.length = 0; // DropDown Menü entleeren 

      //********************* AUSWAHL 2 *****************************************************************   
      if (auswahl2.options[auswahl2.selectedIndex].value == "d") {
        auswahl3.options[0] = new Option("h");
        auswahl3.options[1] = new Option("i");
        auswahl3.options[2] = new Option("j");
      } else if (auswahl2.options[auswahl2.selectedIndex].value == "e") {
        auswahl3.options[0] = new Option("i");
        auswahl3.options[1] = new Option("j");
        auswahl3.options[2] = new Option("k");
      } else if (auswahl2.options[auswahl2.selectedIndex].value == "f") {
        auswahl3.options[0] = new Option("k");
        auswahl3.options[1] = new Option("l");
        auswahl3.options[2] = new Option("m");
      }
    }

     //*********************Auswahl 3 ********************************************************************
    function update_auswahl3() {
        var speicher;
        var auswahl3 = document.forms.verzeichnis.auswahl3;
        var auswahl4 = document.forms.verzeichnis.auswahl4;
        auswahl4.options.length = 0; // DropDown Menü entleeren 

        //**************************************************************************************************   
        if (auswahl3.options[auswahl3.selectedIndex].value == "i") {
          auswahl4.options[0] = new Option("l");
          auswahl4.options[1] = new Option("k");
          auswahl4.options[2] = new Option("m");
        } else if (auswahl3.options[auswahl3.selectedIndex].value == "j") {
          auswahl4.options[0] = new Option("n");
          auswahl4.options[1] = new Option("o");
          auswahl4.options[2] = new Option("p");
        } else if (auswahl3.options[auswahl3.selectedIndex].value == "k") {
          auswahl4.options[0] = new Option("q");
          auswahl4.options[1] = new Option("r");
          auswahl4.options[2] = new Option("s");
        }
      }
      // Ende -->
  </script>
  <title>Unbenanntes Dokument</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
  <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
    Name:
    <input type="text" name="fname">
    <input type="submit" value="Submit">


  </form>
  <form name="verzeichnis">
    <select size="1" name="auswahl1" onChange="update_auswahl1()">
      <option value="" selected>---- Bitte w&auml;hlen ----</option>
      <option value="a">a</option>
      <option value="b">b</option>
      <option value="c">c</option>
    </select>
    <br>
    <br>
    <select size="1" name="auswahl2" onChange="update_auswahl2()">
      <option selected>---- Bitte w&auml;hlen ----</option>
    </select>
    <br>
    <br>
    <select name="auswahl3" size="1" onChange="update_auswahl3()">
      <option selected>---- Bitte w&auml;hlen ----</option>
    </select>
    <br>
    <br>
    <select name="auswahl4" size="1">
      <option selected>---- Bitte w&auml;hlen ----</option>
    </select>
  </form>

</body>

</html>

1

There are 1 answers

0
S.M.V.A On BEST ANSWER
<select size="1" name="auswahl1" onChange="update_auswahl1()">
  <option value="" selected>---- Bitte w&auml;hlen ----</option>
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
</select>

Here you have select box size 4 and last index value is "c". In function you have changed first 3 index values and third index value is 3. So c value is twice. So do following changes index in function

auswahl1.options[1] = new Option("a");
auswahl1.options[2] = new Option("b");
auswahl1.options[3] = new Option("c")

And for second problem onChange event not working, You have not closed the function properly. I have updated the code please check now

<html>

<head>
  <style type="text/css">
  </style>

  <script language="Javascript">
     function validateForm() {
      var x = document.forms["myForm"]["fname"].value;
      var speicher;
      var auswahl1 = document.forms.verzeichnis.auswahl1;
      var auswahl2 = document.forms.verzeichnis.auswahl2;
      var auswahl3 = document.forms.verzeichnis.auswahl3;
      auswahl2.options.length = 0; // DropDown Menü entleeren 
      auswahl3.options.length = 0; // DropDown Menü entleeren
      if (x == "6") {
        alert("Name must be filled out");
        auswahl1.options[1] = new Option("a");
        auswahl1.options[2] = new Option("b");
        auswahl1.options[3] = new Option("c");
        auswahl2.options[0] = new Option("---- Bitte waehlen ----");
        auswahl3.options[0] = new Option("---- Bitte waehlen ----");
        return false;
      }


}
      function update_auswahl1() {
        var speicher;
        var auswahl1 = document.forms.verzeichnis.auswahl1;
        var auswahl2 = document.forms.verzeichnis.auswahl2;
        var auswahl3 = document.forms.verzeichnis.auswahl3;
        auswahl2.options.length = 0; // DropDown Menü entleeren 
        auswahl3.options.length = 0; // DropDown Menü entleeren 

        //********************** AUSWAHL 1 ****************************************************************

        if (auswahl1.options[auswahl1.selectedIndex].value == "a") {
          auswahl2.options[0] = new Option("d");
          auswahl2.options[1] = new Option("e");
        } else if (auswahl1.options[auswahl1.selectedIndex].value == "b") {
          auswahl2.options[0] = new Option("e");
          auswahl2.options[1] = new Option("f");
        } else if (auswahl1.options[auswahl1.selectedIndex].value == "c") {
          auswahl2.options[0] = new Option("f");
          auswahl2.options[1] = new Option("g");
        } else if (auswahl1.options[auswahl1.selectedIndex].value == "") {
          auswahl2.options[0] = new Option("---- Bitte waehlen ----");
        }

        update_auswahl2();
        //*************************************************************************************************
      }


    function update_auswahl2() {
      var speicher;
      var auswahl2 = document.forms.verzeichnis.auswahl2;
      var auswahl3 = document.forms.verzeichnis.auswahl3;
      auswahl3.options.length = 0; // DropDown Menü entleeren 

      //********************* AUSWAHL 2 *****************************************************************   
      if (auswahl2.options[auswahl2.selectedIndex].value == "d") {
        auswahl3.options[0] = new Option("h");
        auswahl3.options[1] = new Option("i");
        auswahl3.options[2] = new Option("j");
      } else if (auswahl2.options[auswahl2.selectedIndex].value == "e") {
        auswahl3.options[0] = new Option("i");
        auswahl3.options[1] = new Option("j");
        auswahl3.options[2] = new Option("k");
      } else if (auswahl2.options[auswahl2.selectedIndex].value == "f") {
        auswahl3.options[0] = new Option("k");
        auswahl3.options[1] = new Option("l");
        auswahl3.options[2] = new Option("m");
      }
    }

     //*********************Auswahl 3 ********************************************************************
    function update_auswahl3() {
        var speicher;
        var auswahl3 = document.forms.verzeichnis.auswahl3;
        var auswahl4 = document.forms.verzeichnis.auswahl4;
        auswahl4.options.length = 0; // DropDown Menü entleeren 

        //**************************************************************************************************   
        if (auswahl3.options[auswahl3.selectedIndex].value == "i") {
          auswahl4.options[0] = new Option("l");
          auswahl4.options[1] = new Option("k");
          auswahl4.options[2] = new Option("m");
        } else if (auswahl3.options[auswahl3.selectedIndex].value == "j") {
          auswahl4.options[0] = new Option("n");
          auswahl4.options[1] = new Option("o");
          auswahl4.options[2] = new Option("p");
        } else if (auswahl3.options[auswahl3.selectedIndex].value == "k") {
          auswahl4.options[0] = new Option("q");
          auswahl4.options[1] = new Option("r");
          auswahl4.options[2] = new Option("s");
        }
      }
      // Ende -->
  </script>
  <title>Unbenanntes Dokument</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
  <form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
    Name:
    <input type="text" name="fname">
    <input type="submit" value="Submit">


  </form>
  <form name="verzeichnis">
    <select size="1" name="auswahl1" onChange="update_auswahl1()">
      <option value="" selected>---- Bitte w&auml;hlen ----</option>
      <option value="a">a</option>
      <option value="b">b</option>
      <option value="c">c</option>
    </select>
    <br>
    <br>
    <select size="1" name="auswahl2" onChange="update_auswahl2()">
      <option selected>---- Bitte w&auml;hlen ----</option>
    </select>
    <br>
    <br>
    <select name="auswahl3" size="1" onChange="update_auswahl3()">
      <option selected>---- Bitte w&auml;hlen ----</option>
    </select>
    <br>
    <br>
    <select name="auswahl4" size="1">
      <option selected>---- Bitte w&auml;hlen ----</option>
    </select>
  </form>

</body>

</html>