如何将环路交换机放入阵列?

时间:2017-02-19 12:15:48

标签: javascript arrays loops

我有一个开关,提示询问数据库。如何将不同的案例添加到我拥有的数组中?我有5个数组,但我不知道输入什么作为输入,因为我使用了一个循环来提示。

<html>
    <head>
    <title>Nested For</title>
        <input type="button" value="Start" onclick="Input()">
        <script type="text/javascript">
            function Input()
                for (var i = 1; i < 5; i++) {
                    for (var b = 0; b <5; i++) {
                        switch(var b) {
                            case 1:
                                contacts[var i] [var b]=prompt ("What is the first name of Customer  " + var i +  "?";)
                                break;
                            case 2:
                                CustomerId[var i] [var b]=prompt ("what is the Customer Id of the Customer " + var i + "?";)
                                break;
                            case 3:
                                Address[var i] [var b]=prompt ("What is the address of the Customer " + var i + "?";)
                                break;
                            case 4:
                                City[var i] [var b]=prompt ("What is the city that the customer lives in " + var i + "?";)
                                break;
                             case 5:
                                Age[var i] [var b]=prompt ("What is the age of the Cusotmer " + var i + "?";)
                                break;
                        }

                        var database = new Array();
                        database[0] = new Array();
                        database[0][0] =
                        database[0][1] =
                        database[0][2] =
                        database[0][3] =
                        database[0][4] =

                        database[1] = new Array();
                        database[1][0] =
                        database[1][1] =
                        database[1][2] =
                        database[1][3] =
                        database[1][4] =

                        database[2] = new Array();
                        database[2][0] =
                        database[2][1] =
                        database[2][2] =
                        database[2][3] =
                        database[2][4] =

                        database[3] = new Array();
                        database[3][0] =
                        database[3][1] =
                        database[3][2] =
                        database[3][3] =
                        database[3][4] =

                        database[4] = new Array();
                        database[4][0] =
                        database[4][1] =
                        database[4][2] =
                        database[4][3] =
                        database[4][4] =

                        txtLabel1.value = database[0][0]
                        txtLabel2.value = database[0][1]
                        txtLabel3.value = database[0][2]
                        txtLabel4.value = database[0][3]
                        txtLabel5.value = database[0][4]

                        txtLabel6.value = database[1][0]
                        txtLabel7.value = database[1][1]
                        txtLabel8.value = database[1][2]
                        txtLabel9.value = database[1][3]
                        txtLabel10.value = database[1][4]

                        txtLabel11.value = database[2][0]
                        txtLabel12.value = database[2][1]
                        txtLabel13.value = database[2][2]
                        txtLabel14.value = database[2][3]
                        txtLabel15.value = database[2][4]

                        txtLabel16.value = database[3][0]
                        txtLabel17.value = database[3][1]
                        txtLabel18.value = database[3][2]
                        txtLabel19.value = database[3][3]
                        txtLabel20.value = database[3][4]

                        txtLabel21.value = database[4][0]
                        txtLabel22.value = database[4][1]
                        txtLabel23.value = database[4][2]
                        txtLabel24.value = database[4][3]
                        txtLabel25.value = database[4][4]
                    }
    </script>
    <table name="OutputTable">
         <tr>
             <td>
                Name: <input type="text" name="txtLabel1" id="txtLabel1">
            </td>
            <td>
                <input type="text" name="txtLabel2" id="txtLabel2">
            </td>
            <td>
                <input type="text" name="txtLabel3" id="txtLabel3">
            </td>
            <td>
                <input type="text" name="txtLabel4" id="txtLabel4">
            </td>
            <td>
                <input type="text" name="txtLabel5" id="txtLabel5">
            </td>
        </tr>
        <tr>
            <td>
                Customer Id: <input type="text" name="txtLabel6" id="txtLabel6">
            </td>
            <td>
                <input type="text" name="txtLabel7" id="txtLabel7">
            </td>
            <td>
                <input type="text" name="txtLabel8" id="txtLabel8">
            </td>
            <td>
                <input type="text" name="txtLabel9" id="txtLabel9">
            </td>
            <td>
                <input type="text" name="txtLabel10" id="txtLabel10">
            </td>
        </tr>
        <tr>
            <td>
                Address: <input type="text" name="txtLabel11" id="txtLabel11">
            </td>
            <td>
                <input type="text" name="txtLabel12" id="txtLabel12">
            </td>
            <td>
                <input type="text" name="txtLabel13" id="txtLabel13">
            </td>
            <td>
                <input type="text" name="txtLabel14" id="txtLabel14">
            </td>
            <td>
                <input type="text" name="txtLabel15" id="txtLabel15">
            </td>
        </tr>
        <tr>
            <td>
                City: <input type="text" name="txtLabel16" id="txtLabel16">
            </td>
            <td>
                <input type="text" name="txtLabel17" id="txtLabel17">
            </td>
            <td>
                <input type="text" name="txtLabel18" id="txtLabel18">
            </td>
            <td>
                <input type="text" name="txtLabel19" id="txtLabel19">
            </td>
            <td>
                <input type="text" name="txtLabel20" id="txtLabel20">
            </td>
        </tr>
        <tr>
            <td>
                Age: <input type="text" name="txtLabel21" id="txtLabel21">
            </td>
            <td>
                <input type="text" name="txtLabel22" id="txtLabel22">
            </td>
            <td>
                <input type="text" name="txtLabel23" id="txtLabel23">
            </td>
            <td>
                <input type="text" name="txtLabel24" id="txtLabel24">
            </td>
            <td>
                <input type="text" name="txtLabel25" id="txtLabel25">
            </td>
        </tr>
    </table>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

直接访问database数组可能更简单,而不是使用中间数组。您可以大大简化代码:

<script type="text/javascript">
        function Input() {
          var database = new Array();
          for (var i = 1; i <= 5; i++) {
            database[i] = new Array();
            for (var b = 1; b <= 5; b++) {
              switch (b) {
                case 1:
                  database[i][b] = prompt("What is the first name of Customer  " + i + "?")
                  break;
                case 2:
                  database[i][b] = prompt("what is the Customer Id of the Customer " + i + "?")
                  break;
                case 3:
                  database[i][b] = prompt("What is the address of the Customer " + i + "?")
                  break;
                case 4:
                  database[i][b] = prompt("What is the city that the customer lives in " + i + "?")
                  break;
                case 5:
                  database[i][b] = prompt("What is the age of the Cusotmer " + i + "?")
                  break;
              }
            }
          }
          txtLabel1.value = database[0][0]
          txtLabel2.value = database[0][1]
          txtLabel3.value = database[0][2]
          txtLabel4.value = database[0][3]
          txtLabel5.value = database[0][4]

          txtLabel6.value = database[1][0]
          txtLabel7.value = database[1][1]
          txtLabel8.value = database[1][2]
          txtLabel9.value = database[1][3]
          txtLabel10.value = database[1][4]

          txtLabel11.value = database[2][0]
          txtLabel12.value = database[2][1]
          txtLabel13.value = database[2][2]
          txtLabel14.value = database[2][3]
          txtLabel15.value = database[2][4]

          txtLabel16.value = database[3][0]
          txtLabel17.value = database[3][1]
          txtLabel18.value = database[3][2]
          txtLabel19.value = database[3][3]
          txtLabel20.value = database[3][4]

          txtLabel21.value = database[4][0]
          txtLabel22.value = database[4][1]
          txtLabel23.value = database[4][2]
          txtLabel24.value = database[4][3]
          txtLabel25.value = database[4][4]
        }
</script>

此外,如果您希望进一步简化代码,可以将文本框的值分配也放在for循环中:

<script type="text/javascript">
        function Input() {
          var database = new Array();
          for (var i = 1; i <= 5; i++) {
            database[i] = new Array();
            for (var b = 1; b <= 5; b++) {
              switch (b) {
                case 1:
                  database[i][b] = prompt("What is the first name of Customer  " + i + "?")
                  break;
                case 2:
                  database[i][b] = prompt("what is the Customer Id of the Customer " + i + "?")
                  break;
                case 3:
                  database[i][b] = prompt("What is the address of the Customer " + i + "?")
                  break;
                case 4:
                  database[i][b] = prompt("What is the city that the customer lives in " + i + "?")
                  break;
                case 5:
                  database[i][b] = prompt("What is the age of the Cusotmer " + i + "?")
                  break;
              }
            }
          }
          var labelCursor = 1;
          for (var i in database) {
            for (var b in database[i]) {
              document.getElementById('txtLabel' + labelCursor).value = database[i][b];
              labelCursor++;
            }
          }
        }
</script>

工作示例:https://jsfiddle.net/mspinks/4uozsk40/3/