简体   繁体   English

将ComboBox选定的项目链接到预制的2D阵列

[英]Link ComboBox selected items to pre-made 2D array

I am trying to populate a textbox with data retrieved from an 2 dimensional array i made. 我正在尝试使用从我制作的二维数组中检索到的数据填充文本框。 I figured out how to populate the textboxs with array data, but im having some trouble doing so based on the selected item from the combo box i made. 我想出了如何用数组数据填充文本框,但是根据我从组合框中选择的项目,这样做有点麻烦。

This is what i have so far: 这是我到目前为止所拥有的:

HTML: HTML:

<select id="Product" name="selectProduct" onchange="choice()">
                <option value="Empty" selected>---------</option>
                <option value="PC_Repair" id="PCR">Hardware Repair</option>
                <option value="Soft_Repair" id="SR">Software Cleanup</option>
                <option value="Home_Support" id="HS">Home Support</option>
              </select>
              <button type="button" onclick="choice()">Enter</button>
              <textarea id="demo"></textarea>

Javascript: Javascript:

   <script type="text/javascript">
            function runArray(){    
                var arr = 
                [
                    ["one", "two", "three"],
                    ["four", "five", "six"],
                    ["seven", "eight", "ninne"]
                ]  
            }

            var selected = document.getElementById("Product");

            var selected = document.getElementById("Product");
        var selectedValue = selected.options[selected.selectedIndex].value;

        function choice(){ 
            if(selected.value == "PC_Repair"){
                alert("I did it!")       
                document.getElementById("demo").value = arr[0][0];
            }
        }
        </script>
    </body>

I am now getting the alert, but not the array contents. 我现在得到警报,但不是数组内容。 This is my first post, so please forgive any mistakes in format or procedure. 这是我的第一篇文章,因此,请原谅任何格式或程序上的错误。 Thank you! 谢谢!

arr is declared inside the function runArray so it won't be accessible from outside, you could perhaps update runArray function to return the array, like below: arrrunArray函数内部声明,因此无法从外部访问它,您可以更新runArray函数以返回数组,如下所示:

function runArray() {
    return [
        ["one", "two", "three"],
        ["four", "five", "six"],
        ["seven", "eight", "ninne"]
    ]
};

and then call it and store the value in arr variable, like so: 然后调用它并将值存储在arr变量中,如下所示:

var arr = runArray();

Below is the updated code: 下面是更新的代码:

function runArray(){    
    return [
        ["one", "two", "three"],
        ["four", "five", "six"],
        ["seven", "eight", "ninne"]
    ]
}

var arr = runArray();

var selected = document.getElementById("Product");

var selectedValue = selected.options[selected.selectedIndex].value;

function choice(){ 
    if(selected.value == "PC_Repair"){
        alert("I did it!")       
        document.getElementById("demo").value = arr[0][0];
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM