需要选择一个新列,并使用带有下拉菜单的onchange函数打印到文本区域

Needing a new column to be chosen and print out to an text area using an onchange function with a drop drop down meun

本文关键字:下拉菜单 onchange 函数 区域 文本 打印 选择 新列 一个      更新时间:2023-09-26

好的,我这里有一个6x6的数组。我遇到的问题是,我需要在下拉列表中使用onchange来只选择所选的列。因此,如果我选择第5列,它将只显示该列中的数字。我花了一些时间在网上寻找解决这个问题的方法,所以我求助于这里的每个人。我只使用java脚本来创建这个太

 <script type="text/javascript">
        // create a var for a 2-D array (6x6)
        var afNumbers = new Array(6)
        var i
        var j
        for (i = 0; i <= 5; i++)
            afNumbers[i] = new Array(6)
        //fill the array by using a nested for loop
        //loop  through each row
        for (i = 0; i <= 5; i++)
        {
            //loop through each item in those row
            for (j = 0; j <= 5 ; j++) {
                afNumbers[i][j] = i + j
            }
        }
        //function to print the array in the text area
        function vPrint() {
            var strOut = ""
            for (i = 0; i <= 5; i++)
            {
                //loop through each item in those row
                for (j = 0; j <= 5 ; j++)
                {
                    strOut += afNumbers[i][j] + "'t"
                }
                strOut += "'r"
            }
            document.getElementById('taOut').value = strOut
        }
        function vDiagonal() {
            var strOut = ""
            for (i = 0; i <= 5; i++) {
                //loop through each item in those row
                for (j = 0; j <= 5 ; j++) {
                    if (i == j)
                        strOut += afNumbers[i][j] + "'t"
                    else strOut += "" + "'t"
                }
                strOut += "'r"
            }
            document.getElementById('taOut').value = strOut
            function vColumn()
            {
                if (document.getElementById('PrintMenu').value == "1")
                {
                    document.getElementById('').innerHTML = afNumbers[j] + strOut
                }
            }                      
        }
    </script>
</head>
<body>
    <form id="frm2DArray" style="text-align:center">
        <h2>Two Dimensional Arrays</h2>
        <input type="button" id="btnPrint" value="Print Array" onclick="vPrint()"/><br /><br />
        <input type="button" id="btnTranspose" value="Print Transpose" /><br /><br />
        <input type="button" id="btnDiagonal" value="Print Diagonal" onclick="vDiagonal()" />
        <h3>Print</h3>
         <select id="PrintMenu" onchange="vColumn()">
            <option value="1">Column 1</option>
            <option value="2">Column 2</option>
            <option value="3">Column 3</option>
            <option value="4">Column 4</option>
            <option value="5">Column 5</option>
       </select>
<br />        
        <textarea id="taOut" rows="40" cols="80"></textarea>        
    </form>
</body>
</html>

首先,我注意到您的代码中缺少令人不安的分号。把这些放在你的陈述后面!

您可以获取打印菜单的值并将其存储在变量中。

var col = document.getElementById('PrintMenu').value;

从那里,您可以使用该值在数组中循环。

afNumbers.forEach(function(entry) {
  document.write(entry[col]);
});

这应该把列中的每个数字都吐出来。