【问题标题】:get the value of the selected option in Typescript获取 Typescript 中选定选项的值
【发布时间】:2020-10-05 03:56:37
【问题描述】:

我正在尝试在没有 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】:

    错误告诉你它不能push到一个未定义的元素,你应该尝试初始化变量arr,比如:

    let arr : Array <string> = [];
    

    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 2020-06-03
      • 1970-01-01
      相关资源
      最近更新 更多