简体   繁体   English

我如何将循环开关放入阵列中?

[英]How would I put my looped switches into a array?

I have a switch that has prompts that ask things of a database. 我有一个开关,它具有询问数据库内容的提示。 How would I add the different cases into the array that I have? 如何将不同的案例添加到我拥有的数组中? I have 5 arrays but I don't know what to put into them as an input because I used a loop for the prompts. 我有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>

It's probably simpler to access your database array directly, instead of using intermediate arrays. 直接访问database数组可能比使用中间数组更简单。 You can greatly simplify your code: 您可以大大简化您的代码:

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

In addition, if you would like to simplify your code further, you can put the value assignment for your text boxes within for loop as well: 另外,如果您想进一步简化代码,也可以将文本框的值分配也放入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>

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

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

相关问题 遍历JSON数组后,如何获取URL更新为不同的API ID? - How can I get my URL to update to different API id's once I've looped through a JSON array? 如何设置jQuery移动翻转开关的数组? - How do I set an array of jQuery mobile flip toggle switches? 我如何让一系列图像出现在我的反应屏幕上? - How would I make an array of images appear on my react screen? 登录时如何将我的用户 ID 放入数组 - How do I put my UserID into an array when I log in 我如何将这段代码放在一起? - How would I put this code together? 如何将条件语句放入此JavaScript? - How would I put a conditional statement into this JavaScript? 如何在特定“ul”元素下获取多个“li”元素并将它们放入数组中? - How would I get multiple "li" elements under a specific "ul" element and put them into an array? 如何使用node.js生成xml文件并将其放在服务器上的公共目录中? - How would I generate an xml file with node.js and put it in a public directory on my server? React&amp;Redux:我在后台有 State,但无法在我的 JSX 中渲染循环数组 - React&Redux: I have State in the background, but can't render the looped-through Array in my JSX 如何使用放置请求更改我的数组 - How can i change my array with a put request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM