【问题标题】:How can I get value of input tag type hidden to ngModel?如何获取隐藏到 ngModel 的输入标签类型的值?
【发布时间】:2019-07-08 17:16:48
【问题描述】:

我试图获取输入标签的值,但我总是收到资产名称。任何人都可以帮助我吗?非常感谢。

<select class="form-control" [(ngModel)]=id name="assetID">
    <option *ngFor="let asset of arrAssets" selected>
        <input type="hidden" value="{{ asset.Id }}" name="id">
        {{ asset.Name }}
    </option>
</select>

【问题讨论】:

  • 为什么你有一个隐藏的输入 inside 一个选择/选项。我认为那不是有效的 HTML。

标签: html angular angular7 angular-ngmodel


【解决方案1】:

您可以通过在 &lt;input&gt; 元素上使用 (input) 来做到这一点,例如:

<select class="form-control" [(ngModel)]=id name="assetID">
     <option *ngFor="let asset of arrAssets; let i = index;" selected>
             <input type="hidden" (input)="inputChanged($event , i)" value="{{ asset.Id }}" name="id">
             {{ asset.Name }}
      </option>
</select>

我已将i 添加为索引,以防您想单独检测每个输入

移动到.ts 文件

export class Home {

  constructor(){}

  inputChanged(eve, i) {

   console.log(eve.target.value); // This should print the value of input element

   console.log(i); // This should print the index of input element which has changed
  }

}

【讨论】:

    猜你喜欢
    • 2018-03-03
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多