【问题标题】:access to checkbox in typescript访问打字稿中的复选框
【发布时间】:2018-11-12 19:29:57
【问题描述】:

我有一个这样的 html 表单:

<form (ngSubmit)="addNewBrand(f)" #f="ngForm">
    <label>name:</label>
    <input type="text" name="name" required ngModel #name="ngModel">

    <label>active:</label>
    <input type="checkbox" name="active" required ngModel #active="ngModel">

    <button type="submit" [disabled]="!f.valid">Save</button>

</form>

在ts文件中:

addNewBrand(form:NgForm){
    console.log(form.value.name + ' -  ' + form.value.active);
}

但我无法访问 ts 文件中的复选框。我该怎么办?

【问题讨论】:

  • 到底是什么问题?我试过了,它可以工作,当未选中活动时,“保存”按钮被禁用。
  • 我想获取checkbox的值,但是不能。
  • 我的方法不对吗?
  • 所以,您想获得复选框的真/假值。但是,当未选中复选框时,保存按钮将被禁用。为什么不删除 [disabled]="!f.valid" 绑定?
  • Nooo,我的问题是,如何在打字稿文件中访问以 html 形式定义的复选框项目?

标签: angular forms typescript checkbox


【解决方案1】:

你的代码应该是

 <input type="checkbox" [ngModel]="active" name="Active"> Active

然后

addNewBrand(createForm: NgForm) {
   console.log(createForm.value.active)
}

【讨论】:

  • 非常感谢 Sajeetharan
猜你喜欢
  • 1970-01-01
  • 2018-02-02
  • 1970-01-01
  • 2022-01-01
  • 2022-01-16
  • 1970-01-01
  • 2017-04-13
  • 2017-09-15
  • 2017-12-19
相关资源
最近更新 更多