带输入的级联菜单

时间:2015-06-03 12:14:08

标签: javascript input drop-down-menu chained-select

我想获得链式选择菜单之间的级联。

当我在输入中写入6时,应该转到第一个下拉菜单。

但我的问题是,首先我得到两次" c"并且我无法从第一个下拉菜单进入下一个菜单。

我感谢每一个帮助



<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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

<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>

这里有选择框大小4,最后一个索引值是&#34; c&#34;。在函数中,您已更改前3个索引值,第三个索引值为3.因此c值为两次。因此,在函数

中执行以下更改索引
auswahl1.options[1] = new Option("a");
auswahl1.options[2] = new Option("b");
auswahl1.options[3] = new Option("c")

对于第二个问题onChange事件无效,您还没有正确关闭该功能。我已经更新了代码,请立即查看

<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>
相关问题