【问题标题】:Checkbox group, submit button only if at least one checkbox is checked Angular 2复选框组,仅在选中至少一个复选框时提交按钮 Angular 2
【发布时间】:2018-07-29 20:12:35
【问题描述】:

我有一个包含 3 个项目的复选框组。我用 Angular 5.0.5 和 Clarity vm 创建了它。我希望我的函数检查是否至少选中了一个复选框,然后转到下一页,这是一个向导。到目前为止,向导打开时不会检查是否至少选择了一个复选框。

我的 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>

我的打字稿看起来像这样:

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) {}

你们能帮我定义一下函数correclty吗?

【问题讨论】:

  • 您必须检查是否检查了每个选择。您可以使用循环或映射并从数组中减少。

标签: javascript angular checkbox angular-validation


【解决方案1】:
this.items.some(i => i.chosen) 

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

【讨论】:

    【解决方案2】:

    使用 JQuery 你可以很简单地做到这一点:

    if ($('#frmTest input:checked').length > 0 ) {
    
      //your code
    
    }
    

    【讨论】:

    • 谢谢,但我需要它与 Typescript/ Angular 一起使用。我试过 if ((this.checked).length > 0 ) { this.wizard.open(); } 但它没有工作
    猜你喜欢
    • 2014-04-28
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多