在Typescript中获取所选选项的值。

问题描述 投票:0回答:1

我试图在没有Angular的情况下,在Typescript中获取所选选项的值。我已经设置了如下的HTML。

<div class="formCol">
    <div class="form-group mt-5 mx-1">
    <select class="form-control" id="firstPoints1">
        <option name="point2" value="coner">coner</option>
        <option name="point2" value="edgePillar">edgePillar</option>
        <option name="point2" value="edge">edge</option>
    </select>
    </div>
</div>

相关的javascript:

<script>
    let el = document.querySelector("#firstPoints1");
    let arr : Array <string>;
    function getSelected (el : HTMLFormElement, arr : Array <string>) {
        for (let i = 0; i < el.length; i++) {
            if (el[i].selected) {
                arr.push(el[i].value);
            }
        }
    }
    function getSelected (el, arr)

但出现了以下错误:

"Uncaught TypeError: Cannot read property 'push' of undefined"

typescript dom
1个回答
0
投票

这个错误告诉你,它无法进行一个 推动 到一个未定义的元素,你应该尝试初始化变量的 胳肢窝,喜欢。

let arr : Array <string> = [];
© www.soinside.com 2019 - 2024. All rights reserved.