Angular 4只是在JavaScript中设置了RadioButton



这必须简单,但我不想使用jQuery。
我需要设置一个特定的广播按钮控件。我有它的ID,所以我尝试了:

let radiobutton = document.getElementById("Standard");
radiobutton.checked = true;

我在网络上找到了示例,就像上面一样但是Typescript/JavaScript给我错误消息:"属性'检查'不存在'htmlelement'不存在

有什么想法?

感谢您的帮助。

而不是进行document.getElementByid,请使用@viewchild和模板变量。这是一个示例:

html:

<input #checkbox1 type="checkbox" [checked]="true" (change)="onChange(1, checkbox1)"/>{{name1}}
<br>
<input #checkbox2 type="checkbox" (change)="onChange(2, checkbox2)"/>{{name2}}

打字稿:

  name1 = 'Angular 2';
  name2 = 'Angular 4';
  @ViewChild("input1") input1;
  @ViewChild("input2") input2;
  ngOnInit(){
    this.input1.checked = true;
    console.log("Checkbox1 is checked -  ", this.input1.checked);
    console.log("Checkbox2 is checked -  ", this.input2.checked);
  }
  onChange(checkbox, item){
    console.log("Checkbox%d was checked", checkbox);
    console.log("Checkbox%d is checked -  ",checkbox, item.checked);
  }

stackblitz示例

最新更新