简体   繁体   English

复选框组,仅当至少选中一个复选框时才提交按钮 Angular 2

[英]Checkbox group, submit button only if at least one checkbox is checked Angular 2

I have a checkbox group with 3 items.我有一个包含 3 个项目的复选框组。 I created it with Angular 5.0.5 and Clarity vm.我用 Angular 5.0.5 和 Clarity vm 创建了它。 I want my function to check if at least one of the checkboxes is checked and THEN go at the next page, which is a wizard.我希望我的函数检查是否至少选中了一个复选框,然后转到下一页,这是一个向导。 Until now the wizard opens without checking if at least one checkbox is selected.到目前为止,向导打开时并未检查是否至少选中了一个复选框。

My html looks like this:我的 html 看起来像这样:

<table class="table">
<tbody>
<tr *ngFor="let item of items">
<td>
<input  type="checkbox" name="allowNext" [(ngModel)]="item.chosen">
</td>
<td>
{{item.name}}
</td>
</tr>
</tbody>
</table>
<br>
</form>
<button [disabled] class="btn btn-primary" (click)="go()"> Next </button>

my Typescript for it looks like this:我的打字稿看起来像这样:

items= [
    {name: 'checkbox1', chosen: false},
    {name: 'checkbox2', chosen: false},
    {name: 'checkbox3', chosen: false}
  ];
 go() {
 if(this.items.chosen === true) {
 this.wizard.open();
 ngOnInit () {}
 constructor( public router: Router) {}

could you guys please help me define the function correclty?你们能帮我定义正确的函数吗?

this.items.some(i => i.chosen) 

将检查是否至少选择了一项

With JQuery you can simple do this :使用 JQuery,您可以简单地做到这一点:

if ($('#frmTest input:checked').length > 0 ) {

  //your code

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM