首页 > 解决方案 > primeng 单选按钮如何更改已选中

问题描述

我正在使用 PrimeNg 单选按钮。默认情况下不选中。我希望能够以编程方式使用 javascript 或 typescript 来检查单选按钮。

我尝试了以下但它不起作用。

<p-radioButton inputId="taken" id="taken" name="groupname" value="true"  class="col-sm-3 pl-3"  [(ngModel)]="modalTaken"></p-radioButton>

document.getElementById("taken").checked=true;

我希望能够更改单选按钮进行检查。因为它在普通单选按钮(不是primeng单选按钮)中工作。

标签: angularprimeng

解决方案


HTML:

<p-radioButton inputId="taken" id="taken" name="groupname" value="true" label='true' class="col-sm-3 pl-3"  [(ngModel)]="modalTaken"></p-radioButton>

TS:

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  modalTaken = 'true';
}

modalTaken必须匹配单选按钮的值。如果您有一个带有值的单选按钮,value='dog'modalTaken='dog'应该看起来像这样。


推荐阅读