【发布时间】:2019-09-02 08:06:13
【问题描述】:
我对使用 Angular 还很陌生,想知道是否有一种简单的方法可以首先检查复选框是否被选中。如果它被选中,我想将它传递给saveOptions 函数。如果选择了多个,我想将它们全部传递并保存到一组选项中。有人可以帮帮我吗?
import { Component } from '@angular/core';
import { forEach } from '@angular/router/src/utils/collection';
import { Options } from 'selenium-webdriver/ie';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
options: any = []
saveOptions(x:any[]){
x.forEach(element => {
this.options.push(element);
});
}
}
<ul class="option-row">
<div class="option-group">
<li><input type="checkbox" id="option-1" value="1" required> <label for="option-1">Option 1</label></li>
<li><input type="checkbox" id="option-2" value="2" required> <label for="option-2">Option 2</label></li>
<li><input type="checkbox" id="option-3" value="3" required> <label for="option-3">Option 3</label></li>
</div>
<!--Want to know how to check if the checkbox is selected and pass the selected options to the method-->
<button type="button" (click)="saveOptions();">Submit</button>
</ul>
【问题讨论】:
-
你可以看到这个答案。它会对你更好。 Click here
标签: html angular typescript